From d03d3bd95b06b82246a51aaa8e424a67eb724037 Mon Sep 17 00:00:00 2001 From: xiongmao86 Date: Fri, 17 Apr 2020 22:01:25 +0800 Subject: [PATCH] Fixes internal lint warning in code base. --- clippy_lints/src/derive.rs | 9 ++- clippy_lints/src/empty_enum.rs | 17 ++--- clippy_lints/src/eta_reduction.rs | 20 +++--- clippy_lints/src/identity_conversion.rs | 53 ++++++++------- clippy_lints/src/int_plus_one.rs | 15 ++--- clippy_lints/src/loops.rs | 59 +++++++++-------- clippy_lints/src/ptr.rs | 17 ++--- clippy_lints/src/returns.rs | 88 +++++++++++++------------ clippy_lints/src/transmute.rs | 26 ++++---- 9 files changed, 153 insertions(+), 151 deletions(-) diff --git a/clippy_lints/src/derive.rs b/clippy_lints/src/derive.rs index 015fd9ed59f..7d34ef157e4 100644 --- a/clippy_lints/src/derive.rs +++ b/clippy_lints/src/derive.rs @@ -1,5 +1,5 @@ use crate::utils::paths; -use crate::utils::{is_automatically_derived, is_copy, match_path, span_lint_and_then}; +use crate::utils::{is_automatically_derived, is_copy, match_path, span_lint_and_note, span_lint_and_then}; use if_chain::if_chain; use rustc_hir::{Item, ItemKind, TraitRef}; use rustc_lint::{LateContext, LateLintPass}; @@ -163,14 +163,13 @@ fn check_copy_clone<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, item: &Item<'_>, trait _ => (), } - span_lint_and_then( + span_lint_and_note( cx, EXPL_IMPL_CLONE_ON_COPY, item.span, "you are implementing `Clone` explicitly on a `Copy` type", - |diag| { - diag.span_note(item.span, "consider deriving `Clone` or removing `Copy`"); - }, + item.span, + "consider deriving `Clone` or removing `Copy`", ); } } diff --git a/clippy_lints/src/empty_enum.rs b/clippy_lints/src/empty_enum.rs index 77ae6dbde72..82c0bf93a7f 100644 --- a/clippy_lints/src/empty_enum.rs +++ b/clippy_lints/src/empty_enum.rs @@ -1,6 +1,6 @@ //! lint when there is an enum with no variants -use crate::utils::span_lint_and_then; +use crate::utils::span_lint_and_help; use rustc_hir::{Item, ItemKind}; use rustc_lint::{LateContext, LateLintPass}; use rustc_session::{declare_lint_pass, declare_tool_lint}; @@ -45,13 +45,14 @@ fn check_item(&mut self, cx: &LateContext<'_, '_>, item: &Item<'_>) { let ty = cx.tcx.type_of(did); let adt = ty.ty_adt_def().expect("already checked whether this is an enum"); if adt.variants.is_empty() { - span_lint_and_then(cx, EMPTY_ENUM, item.span, "enum with no variants", |diag| { - diag.span_help( - item.span, - "consider using the uninhabited type `!` (never type) or a wrapper \ - around it to introduce a type which can't be instantiated", - ); - }); + span_lint_and_then( + cx, + EMPTY_ENUM, + item.span, + "enum with no variants", + "consider using the uninhabited type `!` (never type) or a wrapper around it \ + to introduce a type which can't be instantiated", + ); } } } diff --git a/clippy_lints/src/eta_reduction.rs b/clippy_lints/src/eta_reduction.rs index 3d27d8d5c8a..e3e1136b676 100644 --- a/clippy_lints/src/eta_reduction.rs +++ b/clippy_lints/src/eta_reduction.rs @@ -7,7 +7,8 @@ use rustc_session::{declare_lint_pass, declare_tool_lint}; use crate::utils::{ - implements_trait, is_adjusted, iter_input_pats, snippet_opt, span_lint_and_then, type_is_unsafe_function, + implements_trait, is_adjusted, iter_input_pats, snippet_opt, span_lint_and_sugg, span_lint_and_then, + type_is_unsafe_function, }; declare_clippy_lint! { @@ -131,14 +132,15 @@ fn check_closure(cx: &LateContext<'_, '_>, expr: &Expr<'_>) { if let Some(name) = get_ufcs_type_name(cx, method_def_id, &args[0]); then { - span_lint_and_then(cx, REDUNDANT_CLOSURE_FOR_METHOD_CALLS, expr.span, "redundant closure found", |diag| { - diag.span_suggestion( - expr.span, - "remove closure as shown", - format!("{}::{}", name, path.ident.name), - Applicability::MachineApplicable, - ); - }); + span_lint_and_sugg( + cx, + REDUNDANT_CLOSURE_FOR_METHOD_CALLS, + expr.span, + "redundant closure found", + "remove closure as shown", + format!("{}::{}", name, path.ident.name), + Applicability::MachineApplicable, + ); } ); } diff --git a/clippy_lints/src/identity_conversion.rs b/clippy_lints/src/identity_conversion.rs index 4b7c2c4156e..33a9478f058 100644 --- a/clippy_lints/src/identity_conversion.rs +++ b/clippy_lints/src/identity_conversion.rs @@ -1,5 +1,5 @@ use crate::utils::{ - match_def_path, match_trait_method, paths, same_tys, snippet, snippet_with_macro_callsite, span_lint_and_then, + match_def_path, match_trait_method, paths, same_tys, snippet, snippet_with_macro_callsite, span_lint_and_sugg, }; use rustc_errors::Applicability; use rustc_hir::{Expr, ExprKind, HirId, MatchSource}; @@ -58,14 +58,15 @@ fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr<'_>) { if same_tys(cx, a, b) { let sugg = snippet_with_macro_callsite(cx, args[0].span, "").to_string(); - span_lint_and_then(cx, IDENTITY_CONVERSION, e.span, "identical conversion", |diag| { - diag.span_suggestion( - e.span, - "consider removing `.into()`", - sugg, - Applicability::MachineApplicable, // snippet - ); - }); + span_lint_and_sugg( + cx, + IDENTITY_CONVERSION, + e.span, + "identical conversion", + "consider removing `.into()`", + sugg, + Applicability::MachineApplicable, // snippet + ); } } if match_trait_method(cx, e, &paths::INTO_ITERATOR) && &*name.ident.as_str() == "into_iter" { @@ -73,14 +74,15 @@ fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr<'_>) { let b = cx.tables.expr_ty(&args[0]); if same_tys(cx, a, b) { let sugg = snippet(cx, args[0].span, "").into_owned(); - span_lint_and_then(cx, IDENTITY_CONVERSION, e.span, "identical conversion", |diag| { - diag.span_suggestion( - e.span, - "consider removing `.into_iter()`", - sugg, - Applicability::MachineApplicable, // snippet - ); - }); + span_lint_and_sugg( + cx, + IDENTITY_CONVERSION, + e.span, + "identical conversion", + "consider removing `.into_iter()`", + sugg, + Applicability::MachineApplicable, // snippet + ); } } }, @@ -95,14 +97,15 @@ fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr<'_>) { let sugg = snippet(cx, args[0].span.source_callsite(), "").into_owned(); let sugg_msg = format!("consider removing `{}()`", snippet(cx, path.span, "From::from")); - span_lint_and_then(cx, IDENTITY_CONVERSION, e.span, "identical conversion", |diag| { - diag.span_suggestion( - e.span, - &sugg_msg, - sugg, - Applicability::MachineApplicable, // snippet - ); - }); + span_lint_and_sugg( + cx, + IDENTITY_CONVERSION, + e.span, + "identical conversion", + &sugg_msg, + sugg, + Applicability::MachineApplicable, // snippet + ); } } } diff --git a/clippy_lints/src/int_plus_one.rs b/clippy_lints/src/int_plus_one.rs index 2392ad4d7a1..d5dbd495680 100644 --- a/clippy_lints/src/int_plus_one.rs +++ b/clippy_lints/src/int_plus_one.rs @@ -5,7 +5,7 @@ use rustc_lint::{EarlyContext, EarlyLintPass}; use rustc_session::{declare_lint_pass, declare_tool_lint}; -use crate::utils::{snippet_opt, span_lint_and_then}; +use crate::utils::{snippet_opt, span_lint_and_sugg}; declare_clippy_lint! { /// **What it does:** Checks for usage of `x >= y + 1` or `x - 1 >= y` (and `<=`) in a block @@ -149,19 +149,14 @@ fn generate_recommendation( } fn emit_warning(cx: &EarlyContext<'_>, block: &Expr, recommendation: String) { - span_lint_and_then( + span_lint_and_sugg( cx, INT_PLUS_ONE, block.span, "Unnecessary `>= y + 1` or `x - 1 >=`", - |diag| { - diag.span_suggestion( - block.span, - "change it to", - recommendation, - Applicability::MachineApplicable, // snippet - ); - }, + "change it to", + recommendation, + Applicability::MachineApplicable, // snippet ); } } diff --git a/clippy_lints/src/loops.rs b/clippy_lints/src/loops.rs index 345ae53f845..313a7e8569a 100644 --- a/clippy_lints/src/loops.rs +++ b/clippy_lints/src/loops.rs @@ -2471,45 +2471,50 @@ fn check_needless_collect<'a, 'tcx>(expr: &'tcx Expr<'_>, cx: &LateContext<'a, ' match_type(cx, ty, &paths::HASHMAP) { if method.ident.name == sym!(len) { let span = shorten_needless_collect_span(expr); - span_lint_and_then(cx, NEEDLESS_COLLECT, span, NEEDLESS_COLLECT_MSG, |diag| { - diag.span_suggestion( - span, + span_lint_and_sugg(cx, + NEEDLESS_COLLECT, + span, + NEEDLESS_COLLECT_MSG, "replace with", ".count()".to_string(), Applicability::MachineApplicable, ); - }); } if method.ident.name == sym!(is_empty) { let span = shorten_needless_collect_span(expr); - span_lint_and_then(cx, NEEDLESS_COLLECT, span, NEEDLESS_COLLECT_MSG, |diag| { - diag.span_suggestion( - span, - "replace with", - ".next().is_none()".to_string(), - Applicability::MachineApplicable, + span_lint_and_sugg(cx, + NEEDLESS_COLLECT, + span, + NEEDLESS_COLLECT_MSG, + "replace with", + ".next().is_none()".to_string(), + Applicability::MachineApplicable, ); - }); } if method.ident.name == sym!(contains) { let contains_arg = snippet(cx, args[1].span, "??"); let span = shorten_needless_collect_span(expr); - span_lint_and_then(cx, NEEDLESS_COLLECT, span, NEEDLESS_COLLECT_MSG, |diag| { - let (arg, pred) = if contains_arg.starts_with('&') { - ("x", &contains_arg[1..]) - } else { - ("&x", &*contains_arg) - }; - diag.span_suggestion( - span, - "replace with", - format!( - ".any(|{}| x == {})", - arg, pred - ), - Applicability::MachineApplicable, - ); - }); + span_lint_and_then(cx, + NEEDLESS_COLLECT, + span, + NEEDLESS_COLLECT_MSG, + |db| { + let (arg, pred) = if contains_arg.starts_with('&') { + ("x", &contains_arg[1..]) + } else { + ("&x", &*contains_arg) + }; + db.span_suggestion( + span, + "replace with", + format!( + ".any(|{}| x == {})", + arg, pred + ), + Applicability::MachineApplicable, + ); + } + ); } } } diff --git a/clippy_lints/src/ptr.rs b/clippy_lints/src/ptr.rs index c190ed42e3f..1e2afb7a674 100644 --- a/clippy_lints/src/ptr.rs +++ b/clippy_lints/src/ptr.rs @@ -2,8 +2,8 @@ use crate::utils::ptr::get_spans; use crate::utils::{ - is_type_diagnostic_item, match_qpath, match_type, paths, snippet_opt, span_lint, span_lint_and_then, - walk_ptrs_hir_ty, + is_type_diagnostic_item, match_qpath, match_type, paths, snippet_opt, span_lint, span_lint_and_sugg, + span_lint_and_then, walk_ptrs_hir_ty, }; use if_chain::if_chain; use rustc_errors::Applicability; @@ -234,19 +234,14 @@ fn check_fn(cx: &LateContext<'_, '_>, decl: &FnDecl<'_>, fn_id: HirId, opt_body_ then { let replacement = snippet_opt(cx, inner.span); if let Some(r) = replacement { - span_lint_and_then( + span_lint_and_sugg( cx, PTR_ARG, arg.span, "using a reference to `Cow` is not recommended.", - |diag| { - diag.span_suggestion( - arg.span, - "change this to", - "&".to_owned() + &r, - Applicability::Unspecified, - ); - }, + "change this to", + "&".to_owned() + &r, + Applicability::Unspecified, ); } } diff --git a/clippy_lints/src/returns.rs b/clippy_lints/src/returns.rs index f7ab00b7304..3229b244296 100644 --- a/clippy_lints/src/returns.rs +++ b/clippy_lints/src/returns.rs @@ -8,7 +8,7 @@ use rustc_span::source_map::Span; use rustc_span::BytePos; -use crate::utils::{in_macro, match_path_ast, snippet_opt, span_lint_and_then}; +use crate::utils::{in_macro, match_path_ast, snippet_opt, span_lint_and_sugg, span_lint_and_then}; declare_clippy_lint! { /// **What it does:** Checks for return statements at the end of a block. @@ -162,24 +162,26 @@ fn emit_return_lint(cx: &EarlyContext<'_>, ret_span: Span, inner_span: Option match replacement { RetReplacement::Empty => { - span_lint_and_then(cx, NEEDLESS_RETURN, ret_span, "unneeded `return` statement", |diag| { - diag.span_suggestion( - ret_span, - "remove `return`", - String::new(), - Applicability::MachineApplicable, - ); - }); + span_lint_and_sugg( + cx, + NEEDLESS_RETURN, + ret_span, + "unneeded `return` statement", + "remove `return`", + String::new(), + Applicability::MachineApplicable, + ); }, RetReplacement::Block => { - span_lint_and_then(cx, NEEDLESS_RETURN, ret_span, "unneeded `return` statement", |diag| { - diag.span_suggestion( - ret_span, - "replace `return` with an empty block", - "{}".to_string(), - Applicability::MachineApplicable, - ); - }); + span_lint_and_sugg( + cx, + NEEDLESS_RETURN, + ret_span, + "unneeded `return` statement", + "replace `return` with an empty block", + "{}".to_string(), + Applicability::MachineApplicable, + ); }, }, } @@ -259,14 +261,15 @@ fn check_fn(&mut self, cx: &EarlyContext<'_>, kind: FnKind<'_>, span: Span, _: a } else { (ty.span, Applicability::MaybeIncorrect) }; - span_lint_and_then(cx, UNUSED_UNIT, rspan, "unneeded unit return type", |diag| { - diag.span_suggestion( - rspan, - "remove the `-> ()`", - String::new(), - appl, - ); - }); + span_lint_and_sugg( + cx, + UNUSED_UNIT, + rspan, + "unneeded unit return type", + "remove the `-> ()`", + String::new(), + appl, + ); } } } @@ -279,14 +282,16 @@ fn check_block(&mut self, cx: &EarlyContext<'_>, block: &ast::Block) { if is_unit_expr(expr) && !stmt.span.from_expansion(); then { let sp = expr.span; - span_lint_and_then(cx, UNUSED_UNIT, sp, "unneeded unit expression", |diag| { - diag.span_suggestion( - sp, - "remove the final `()`", - String::new(), - Applicability::MachineApplicable, - ); - }); + span_lint_and_sugg( + cx, + UNUSED_UNIT, + sp, + "unneeded unit expression", + "remove the final `()`", + String::new(), + Applicability::MachineApplicable, + ); + } } } @@ -295,14 +300,15 @@ fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &ast::Expr) { match e.kind { ast::ExprKind::Ret(Some(ref expr)) | ast::ExprKind::Break(_, Some(ref expr)) => { if is_unit_expr(expr) && !expr.span.from_expansion() { - span_lint_and_then(cx, UNUSED_UNIT, expr.span, "unneeded `()`", |diag| { - diag.span_suggestion( - expr.span, - "remove the `()`", - String::new(), - Applicability::MachineApplicable, - ); - }); + span_lint_and_sugg( + cx, + UNUSED_UNIT, + expr.span, + "unneeded `()`", + "remove the `()`", + String::new(), + Applicability::MachineApplicable, + ); } }, _ => (), diff --git a/clippy_lints/src/transmute.rs b/clippy_lints/src/transmute.rs index b99d583c4be..e24d2c4f495 100644 --- a/clippy_lints/src/transmute.rs +++ b/clippy_lints/src/transmute.rs @@ -1,5 +1,6 @@ use crate::utils::{ - is_normalizable, last_path_segment, match_def_path, paths, snippet, span_lint, span_lint_and_then, sugg, + is_normalizable, last_path_segment, match_def_path, paths, snippet, span_lint, span_lint_and_sugg, + span_lint_and_then, sugg, }; use if_chain::if_chain; use rustc_ast::ast; @@ -441,24 +442,19 @@ fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr<'_>) { "" }; - span_lint_and_then( + span_lint_and_sugg( cx, TRANSMUTE_BYTES_TO_STR, e.span, &format!("transmute from a `{}` to a `{}`", from_ty, to_ty), - |diag| { - diag.span_suggestion( - e.span, - "consider using", - format!( - "std::str::from_utf8{}({}).unwrap()", - postfix, - snippet(cx, args[0].span, ".."), - ), - Applicability::Unspecified, - ); - } - ) + "consider using", + format!( + "std::str::from_utf8{}({}).unwrap()", + postfix, + snippet(cx, args[0].span, ".."), + ), + Applicability::Unspecified, + ); } else { if cx.tcx.erase_regions(&from_ty) != cx.tcx.erase_regions(&to_ty) { span_lint_and_then(