]> git.lizzy.rs Git - rust.git/blobdiff - clippy_lints/src/methods.rs
Use span_suggestion_with_applicability instead of span_suggestion
[rust.git] / clippy_lints / src / methods.rs
index 1c31c414d2b3747efd8ddb6c5999e34b35924ffc..f61995cf26509cd472c64c57fc13ae855da609f6 100644 (file)
@@ -1,15 +1,15 @@
 use matches::matches;
-use rustc::hir;
-use rustc::lint::*;
-use rustc::{declare_lint, lint_array};
+use crate::rustc::hir;
+use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass, in_external_macro, Lint, LintContext};
+use crate::rustc::{declare_tool_lint, lint_array};
 use if_chain::if_chain;
-use rustc::ty::{self, Ty};
-use rustc::hir::def::Def;
+use crate::rustc::ty::{self, Ty};
+use crate::rustc::hir::def::Def;
 use std::borrow::Cow;
 use std::fmt;
 use std::iter;
-use syntax::ast;
-use syntax::source_map::{Span, BytePos};
+use crate::syntax::ast;
+use crate::syntax::source_map::{Span, BytePos};
 use crate::utils::{get_arg_name, get_trait_def_id, implements_trait, in_macro, is_copy, is_expn_of, is_self,
             is_self_ty, iter_input_pats, last_path_segment, match_def_path, match_path, match_qpath, match_trait_method,
             match_type, method_chain_args, match_var, return_ty, remove_blocks, same_tys, single_segment_path, snippet,
@@ -17,6 +17,7 @@
 use crate::utils::paths;
 use crate::utils::sugg;
 use crate::consts::{constant, Constant};
+use crate::rustc_errors::Applicability;
 
 #[derive(Clone)]
 pub struct Pass;
 ///
 /// **Example:**
 /// ```rust
-/// foo.expect(&format("Err {}: {}", err_code, err_msg))
+/// foo.expect(&format!("Err {}: {}", err_code, err_msg))
 /// ```
 /// or
 /// ```rust
-/// foo.expect(format("Err {}: {}", err_code, err_msg).as_str())
+/// foo.expect(format!("Err {}: {}", err_code, err_msg).as_str())
 /// ```
 /// this can instead be written:
 /// ```rust
 /// foo.unwrap_or_else(|_| panic!("Err {}: {}", err_code, err_msg))
 /// ```
-/// or
-/// ```rust
-/// foo.unwrap_or_else(|_| panic!(format("Err {}: {}", err_code, err_msg).as_str()))
-/// ```
 declare_clippy_lint! {
     pub EXPECT_FUN_CALL,
     perf,
@@ -714,7 +711,7 @@ fn get_lints(&self) -> LintArray {
 }
 
 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
-    #[allow(cyclomatic_complexity)]
+    #[allow(clippy::cyclomatic_complexity)]
     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) {
         if in_macro(expr.span) {
             return;
@@ -922,7 +919,7 @@ fn check_unwrap_or_default(
     }
 
     /// Check for `*or(foo())`.
-    #[allow(too_many_arguments)]
+    #[allow(clippy::too_many_arguments)]
     fn check_general_case(
         cx: &LateContext<'_, '_>,
         name: &str,
@@ -1131,8 +1128,18 @@ fn lint_clone_on_copy(cx: &LateContext<'_, '_>, expr: &hir::Expr, arg: &hir::Exp
                     let refs: String = iter::repeat('&').take(n + 1).collect();
                     let derefs: String = iter::repeat('*').take(n).collect();
                     let explicit = format!("{}{}::clone({})", refs, ty, snip);
-                    db.span_suggestion(expr.span, "try dereferencing it", format!("{}({}{}).clone()", refs, derefs, snip.deref()));
-                    db.span_suggestion(expr.span, "or try being explicit about what type to clone", explicit);
+                    db.span_suggestion_with_applicability(
+                            expr.span,
+                            "try dereferencing it",
+                            format!("{}({}{}).clone()", refs, derefs, snip.deref()),
+                            Applicability::Unspecified,
+                            );
+                    db.span_suggestion_with_applicability(
+                            expr.span, 
+                            "or try being explicit about what type to clone", 
+                            explicit,
+                            Applicability::Unspecified,
+                            );
                 },
             );
             return; // don't report clone_on_copy
@@ -1145,14 +1152,14 @@ fn lint_clone_on_copy(cx: &LateContext<'_, '_>, expr: &hir::Expr, arg: &hir::Exp
             if let ty::Ref(..) = cx.tables.expr_ty(arg).sty {
                 let parent = cx.tcx.hir.get_parent_node(expr.id);
                 match cx.tcx.hir.get(parent) {
-                    hir::map::NodeExpr(parent) => match parent.node {
+                    hir::Node::Expr(parent) => match parent.node {
                         // &*x is a nop, &x.clone() is not
                         hir::ExprKind::AddrOf(..) |
                         // (*x).func() is useless, x.clone().func() can work in case func borrows mutably
                         hir::ExprKind::MethodCall(..) => return,
                         _ => {},
                     }
-                    hir::map::NodeStmt(stmt) => {
+                    hir::Node::Stmt(stmt) => {
                         if let hir::StmtKind::Decl(ref decl, _) = stmt.node {
                             if let hir::DeclKind::Local(ref loc) = decl.node {
                                 if let hir::PatKind::Ref(..) = loc.pat.node {
@@ -1173,7 +1180,12 @@ fn lint_clone_on_copy(cx: &LateContext<'_, '_>, expr: &hir::Expr, arg: &hir::Exp
         }
         span_lint_and_then(cx, CLONE_ON_COPY, expr.span, "using `clone` on a `Copy` type", |db| {
             if let Some((text, snip)) = snip {
-                db.span_suggestion(expr.span, text, snip);
+                db.span_suggestion_with_applicability(
+                    expr.span,
+                    text,
+                    snip,
+                    Applicability::Unspecified,
+                    );
             }
         });
     }
@@ -1643,7 +1655,12 @@ fn lint_map_or_none<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr,
             let map_or_func_snippet = snippet(cx, map_or_args[2].span, "..");
             let hint = format!("{0}.and_then({1})", map_or_self_snippet, map_or_func_snippet);
             span_lint_and_then(cx, OPTION_MAP_OR_NONE, expr.span, msg, |db| {
-                db.span_suggestion(expr.span, "try using and_then instead", hint);
+                db.span_suggestion_with_applicability(
+                        expr.span,
+                        "try using and_then instead",
+                        hint,
+                        Applicability::Unspecified,
+                        );
             });
         }
     }