]> git.lizzy.rs Git - rust.git/blobdiff - clippy_lints/src/formatting.rs
Auto merge of #3946 - rchaser53:issue-3920, r=flip1995
[rust.git] / clippy_lints / src / formatting.rs
index cc3801423c88e886eeca2c3ae0e45e45aff966a7..08376bc49d22488283c62e08db6358cdea6ae47a 100644 (file)
@@ -1,87 +1,78 @@
-// Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
-// file at the top-level directory of this distribution.
-//
-// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
-// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
-
-use crate::rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
-use crate::rustc::{declare_tool_lint, lint_array};
-use crate::syntax::ast;
-use crate::syntax::ptr::P;
 use crate::utils::{differing_macro_contexts, in_macro, snippet_opt, span_note_and_lint};
+use rustc::lint::{in_external_macro, EarlyContext, EarlyLintPass, LintArray, LintPass};
+use rustc::{declare_tool_lint, lint_array};
+use syntax::ast;
+use syntax::ptr::P;
 
-/// **What it does:** Checks for use of the non-existent `=*`, `=!` and `=-`
-/// operators.
-///
-/// **Why is this bad?** This is either a typo of `*=`, `!=` or `-=` or
-/// confusing.
-///
-/// **Known problems:** None.
-///
-/// **Example:**
-/// ```rust,ignore
-/// a =- 42; // confusing, should it be `a -= 42` or `a = -42`?
-/// ```
 declare_clippy_lint! {
+    /// **What it does:** Checks for use of the non-existent `=*`, `=!` and `=-`
+    /// operators.
+    ///
+    /// **Why is this bad?** This is either a typo of `*=`, `!=` or `-=` or
+    /// confusing.
+    ///
+    /// **Known problems:** None.
+    ///
+    /// **Example:**
+    /// ```rust,ignore
+    /// a =- 42; // confusing, should it be `a -= 42` or `a = -42`?
+    /// ```
     pub SUSPICIOUS_ASSIGNMENT_FORMATTING,
     style,
     "suspicious formatting of `*=`, `-=` or `!=`"
 }
 
-/// **What it does:** Checks for formatting of `else`. It lints if the `else`
-/// is followed immediately by a newline or the `else` seems to be missing.
-///
-/// **Why is this bad?** This is probably some refactoring remnant, even if the
-/// code is correct, it might look confusing.
-///
-/// **Known problems:** None.
-///
-/// **Example:**
-/// ```rust,ignore
-/// if foo {
-/// } { // looks like an `else` is missing here
-/// }
-///
-/// if foo {
-/// } if bar { // looks like an `else` is missing here
-/// }
-///
-/// if foo {
-/// } else
-///
-/// { // this is the `else` block of the previous `if`, but should it be?
-/// }
-///
-/// if foo {
-/// } else
-///
-/// if bar { // this is the `else` block of the previous `if`, but should it be?
-/// }
-/// ```
 declare_clippy_lint! {
+    /// **What it does:** Checks for formatting of `else`. It lints if the `else`
+    /// is followed immediately by a newline or the `else` seems to be missing.
+    ///
+    /// **Why is this bad?** This is probably some refactoring remnant, even if the
+    /// code is correct, it might look confusing.
+    ///
+    /// **Known problems:** None.
+    ///
+    /// **Example:**
+    /// ```rust,ignore
+    /// if foo {
+    /// } { // looks like an `else` is missing here
+    /// }
+    ///
+    /// if foo {
+    /// } if bar { // looks like an `else` is missing here
+    /// }
+    ///
+    /// if foo {
+    /// } else
+    ///
+    /// { // this is the `else` block of the previous `if`, but should it be?
+    /// }
+    ///
+    /// if foo {
+    /// } else
+    ///
+    /// if bar { // this is the `else` block of the previous `if`, but should it be?
+    /// }
+    /// ```
     pub SUSPICIOUS_ELSE_FORMATTING,
     style,
     "suspicious formatting of `else`"
 }
 
-/// **What it does:** Checks for possible missing comma in an array. It lints if
-/// an array element is a binary operator expression and it lies on two lines.
-///
-/// **Why is this bad?** This could lead to unexpected results.
-///
-/// **Known problems:** None.
-///
-/// **Example:**
-/// ```rust,ignore
-/// let a = &[
-///     -1, -2, -3 // <= no comma here
-///     -4, -5, -6
-/// ];
-/// ```
 declare_clippy_lint! {
+    /// **What it does:** Checks for possible missing comma in an array. It lints if
+    /// an array element is a binary operator expression and it lies on two lines.
+    ///
+    /// **Why is this bad?** This could lead to unexpected results.
+    ///
+    /// **Known problems:** None.
+    ///
+    /// **Example:**
+    /// ```rust,ignore
+    /// let a = &[
+    ///     -1, -2, -3 // <= no comma here
+    ///     -4, -5, -6
+    /// ];
+    /// ```
     pub POSSIBLE_MISSING_COMMA,
     correctness,
     "possible missing comma in array"
@@ -98,6 +89,10 @@ fn get_lints(&self) -> LintArray {
             POSSIBLE_MISSING_COMMA
         )
     }
+
+    fn name(&self) -> &'static str {
+        "Formatting"
+    }
 }
 
 impl EarlyLintPass for Formatting {
@@ -155,6 +150,7 @@ fn check_else(cx: &EarlyContext<'_>, expr: &ast::Expr) {
         if (is_block(else_) || unsugar_if(else_).is_some())
             && !differing_macro_contexts(then.span, else_.span)
             && !in_macro(then.span)
+            && !in_external_macro(cx.sess, expr.span)
         {
             // workaround for rust-lang/rust#43081
             if expr.span.lo().0 == 0 && expr.span.hi().0 == 0 {