]> git.lizzy.rs Git - rust.git/blobdiff - clippy_lints/src/misc.rs
Allow allowing of toplevel_ref_arg lint
[rust.git] / clippy_lints / src / misc.rs
index 77aee50c6f872d2bb76200d3a3e85a441032010b..6e41249ea6488e04aff575125225fe899095746b 100644 (file)
@@ -4,7 +4,7 @@
 use rustc::hir::*;
 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
 use rustc::ty;
-use rustc::{declare_tool_lint, lint_array};
+use rustc::{declare_lint_pass, declare_tool_lint};
 use rustc_errors::Applicability;
 use syntax::ast::LitKind;
 use syntax::source_map::{ExpnFormat, Span};
@@ -13,8 +13,8 @@
 use crate::utils::sugg::Sugg;
 use crate::utils::{
     get_item_name, get_parent_expr, implements_trait, in_constant, in_macro, is_integer_literal, iter_input_pats,
-    last_path_segment, match_qpath, match_trait_method, paths, snippet, span_lint, span_lint_and_then, walk_ptrs_ty,
-    SpanlessEq,
+    last_path_segment, match_qpath, match_trait_method, paths, snippet, span_lint, span_lint_and_then,
+    span_lint_hir_and_then, walk_ptrs_ty, SpanlessEq,
 };
 
 declare_clippy_lint! {
     /// **Known problems:** None.
     ///
     /// **Example:**
-    /// ```ignore
-    /// x == NAN
+    /// ```rust
+    /// # use core::f32::NAN;
+    /// # let x = 1.0;
+    ///
+    /// if x == NAN { }
     /// ```
     pub CMP_NAN,
     correctness,
     /// **Known problems:** None.
     ///
     /// **Example:**
-    /// ```ignore
-    /// y == 1.23f64
-    /// y != x  // where both are floats
+    /// ```rust
+    /// let x = 1.2331f64;
+    /// let y = 1.2332f64;
+    /// if y == 1.23f64 { }
+    /// if y != x {} // where both are floats
     /// ```
     pub FLOAT_CMP,
     correctness,
     /// **Known problems:** None.
     ///
     /// **Example:**
-    /// ```ignore
-    /// x % 1
+    /// ```rust
+    /// # let x = 1;
+    /// let a = x % 1;
     /// ```
     pub MODULO_ONE,
     correctness,
     /// **Known problems:** None.
     ///
     /// **Example:**
-    /// ```ignore
+    /// ```rust
+    /// # let v = Some("abc");
+    ///
     /// match v {
     ///     Some(x) => (),
     ///     y @ _ => (), // easier written as `y`,
     ///
     /// **Example:**
     ///
-    /// ```ignore
-    /// 0 as *const u32
+    /// ```rust
+    /// let a = 0 as *const u32;
     /// ```
     pub ZERO_PTR,
     style,
     "using `==` or `!=` on float constants instead of comparing difference with an epsilon"
 }
 
-#[derive(Copy, Clone)]
-pub struct Pass;
-
-impl LintPass for Pass {
-    fn get_lints(&self) -> LintArray {
-        lint_array!(
-            TOPLEVEL_REF_ARG,
-            CMP_NAN,
-            FLOAT_CMP,
-            CMP_OWNED,
-            MODULO_ONE,
-            REDUNDANT_PATTERN,
-            USED_UNDERSCORE_BINDING,
-            SHORT_CIRCUIT_STATEMENT,
-            ZERO_PTR,
-            FLOAT_CMP_CONST
-        )
-    }
-
-    fn name(&self) -> &'static str {
-        "MiscLints"
-    }
-}
-
-impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
+declare_lint_pass!(MiscLints => [
+    TOPLEVEL_REF_ARG,
+    CMP_NAN,
+    FLOAT_CMP,
+    CMP_OWNED,
+    MODULO_ONE,
+    REDUNDANT_PATTERN,
+    USED_UNDERSCORE_BINDING,
+    SHORT_CIRCUIT_STATEMENT,
+    ZERO_PTR,
+    FLOAT_CMP_CONST
+]);
+
+impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MiscLints {
     fn check_fn(
         &mut self,
         cx: &LateContext<'a, 'tcx>,
@@ -285,19 +282,20 @@ fn check_stmt(&mut self, cx: &LateContext<'a, 'tcx>, s: &'tcx Stmt) {
             if let Some(ref init) = l.init;
             then {
                 if an == BindingAnnotation::Ref || an == BindingAnnotation::RefMut {
-                    let init = Sugg::hir(cx, init, "..");
+                    let sugg_init = Sugg::hir(cx, init, "..");
                     let (mutopt,initref) = if an == BindingAnnotation::RefMut {
-                        ("mut ", init.mut_addr())
+                        ("mut ", sugg_init.mut_addr())
                     } else {
-                        ("", init.addr())
+                        ("", sugg_init.addr())
                     };
                     let tyopt = if let Some(ref ty) = l.ty {
                         format!(": &{mutopt}{ty}", mutopt=mutopt, ty=snippet(cx, ty.span, "_"))
                     } else {
                         String::new()
                     };
-                    span_lint_and_then(cx,
+                    span_lint_hir_and_then(cx,
                         TOPLEVEL_REF_ARG,
+                        init.hir_id,
                         l.pat.span,
                         "`ref` on an entire `let` pattern is discouraged, take a reference with `&` instead",
                         |db| {
@@ -604,7 +602,7 @@ fn in_attributes_expansion(expr: &Expr) -> bool {
 /// Tests whether `def` is a variable defined outside a macro.
 fn non_macro_local(cx: &LateContext<'_, '_>, def: &def::Def) -> bool {
     match *def {
-        def::Def::Local(id) | def::Def::Upvar(id, _, _) => !in_macro(cx.tcx.hir().span(id)),
+        def::Def::Local(id) | def::Def::Upvar(id, _, _) => !in_macro(cx.tcx.hir().span_by_hir_id(id)),
         _ => false,
     }
 }