Fixes internal lint warning in code base.

This commit is contained in:
xiongmao86 2020-04-17 22:01:25 +08:00
parent bdd32e7700
commit d03d3bd95b
9 changed files with 153 additions and 151 deletions

View File

@ -1,5 +1,5 @@
use crate::utils::paths; 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 if_chain::if_chain;
use rustc_hir::{Item, ItemKind, TraitRef}; use rustc_hir::{Item, ItemKind, TraitRef};
use rustc_lint::{LateContext, LateLintPass}; 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, cx,
EXPL_IMPL_CLONE_ON_COPY, EXPL_IMPL_CLONE_ON_COPY,
item.span, item.span,
"you are implementing `Clone` explicitly on a `Copy` type", "you are implementing `Clone` explicitly on a `Copy` type",
|diag| { item.span,
diag.span_note(item.span, "consider deriving `Clone` or removing `Copy`"); "consider deriving `Clone` or removing `Copy`",
},
); );
} }
} }

View File

@ -1,6 +1,6 @@
//! lint when there is an enum with no variants //! 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_hir::{Item, ItemKind};
use rustc_lint::{LateContext, LateLintPass}; use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_session::{declare_lint_pass, declare_tool_lint};
@ -45,13 +45,14 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EmptyEnum {
let ty = cx.tcx.type_of(did); let ty = cx.tcx.type_of(did);
let adt = ty.ty_adt_def().expect("already checked whether this is an enum"); let adt = ty.ty_adt_def().expect("already checked whether this is an enum");
if adt.variants.is_empty() { if adt.variants.is_empty() {
span_lint_and_then(cx, EMPTY_ENUM, item.span, "enum with no variants", |diag| { span_lint_and_then(
diag.span_help( cx,
item.span, EMPTY_ENUM,
"consider using the uninhabited type `!` (never type) or a wrapper \ item.span,
around it to introduce a type which can't be instantiated", "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",
);
} }
} }
} }

View File

@ -7,7 +7,8 @@ use rustc_middle::ty::{self, Ty};
use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_session::{declare_lint_pass, declare_tool_lint};
use crate::utils::{ 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! { 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]); if let Some(name) = get_ufcs_type_name(cx, method_def_id, &args[0]);
then { then {
span_lint_and_then(cx, REDUNDANT_CLOSURE_FOR_METHOD_CALLS, expr.span, "redundant closure found", |diag| { span_lint_and_sugg(
diag.span_suggestion( cx,
expr.span, REDUNDANT_CLOSURE_FOR_METHOD_CALLS,
"remove closure as shown", expr.span,
format!("{}::{}", name, path.ident.name), "redundant closure found",
Applicability::MachineApplicable, "remove closure as shown",
); format!("{}::{}", name, path.ident.name),
}); Applicability::MachineApplicable,
);
} }
); );
} }

View File

@ -1,5 +1,5 @@
use crate::utils::{ 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_errors::Applicability;
use rustc_hir::{Expr, ExprKind, HirId, MatchSource}; use rustc_hir::{Expr, ExprKind, HirId, MatchSource};
@ -58,14 +58,15 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IdentityConversion {
if same_tys(cx, a, b) { if same_tys(cx, a, b) {
let sugg = snippet_with_macro_callsite(cx, args[0].span, "<expr>").to_string(); let sugg = snippet_with_macro_callsite(cx, args[0].span, "<expr>").to_string();
span_lint_and_then(cx, IDENTITY_CONVERSION, e.span, "identical conversion", |diag| { span_lint_and_sugg(
diag.span_suggestion( cx,
e.span, IDENTITY_CONVERSION,
"consider removing `.into()`", e.span,
sugg, "identical conversion",
Applicability::MachineApplicable, // snippet "consider removing `.into()`",
); sugg,
}); Applicability::MachineApplicable, // snippet
);
} }
} }
if match_trait_method(cx, e, &paths::INTO_ITERATOR) && &*name.ident.as_str() == "into_iter" { if match_trait_method(cx, e, &paths::INTO_ITERATOR) && &*name.ident.as_str() == "into_iter" {
@ -73,14 +74,15 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IdentityConversion {
let b = cx.tables.expr_ty(&args[0]); let b = cx.tables.expr_ty(&args[0]);
if same_tys(cx, a, b) { if same_tys(cx, a, b) {
let sugg = snippet(cx, args[0].span, "<expr>").into_owned(); let sugg = snippet(cx, args[0].span, "<expr>").into_owned();
span_lint_and_then(cx, IDENTITY_CONVERSION, e.span, "identical conversion", |diag| { span_lint_and_sugg(
diag.span_suggestion( cx,
e.span, IDENTITY_CONVERSION,
"consider removing `.into_iter()`", e.span,
sugg, "identical conversion",
Applicability::MachineApplicable, // snippet "consider removing `.into_iter()`",
); sugg,
}); Applicability::MachineApplicable, // snippet
);
} }
} }
}, },
@ -95,14 +97,15 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IdentityConversion {
let sugg = snippet(cx, args[0].span.source_callsite(), "<expr>").into_owned(); let sugg = snippet(cx, args[0].span.source_callsite(), "<expr>").into_owned();
let sugg_msg = let sugg_msg =
format!("consider removing `{}()`", snippet(cx, path.span, "From::from")); format!("consider removing `{}()`", snippet(cx, path.span, "From::from"));
span_lint_and_then(cx, IDENTITY_CONVERSION, e.span, "identical conversion", |diag| { span_lint_and_sugg(
diag.span_suggestion( cx,
e.span, IDENTITY_CONVERSION,
&sugg_msg, e.span,
sugg, "identical conversion",
Applicability::MachineApplicable, // snippet &sugg_msg,
); sugg,
}); Applicability::MachineApplicable, // snippet
);
} }
} }
} }

View File

@ -5,7 +5,7 @@ use rustc_errors::Applicability;
use rustc_lint::{EarlyContext, EarlyLintPass}; use rustc_lint::{EarlyContext, EarlyLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint}; 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! { declare_clippy_lint! {
/// **What it does:** Checks for usage of `x >= y + 1` or `x - 1 >= y` (and `<=`) in a block /// **What it does:** Checks for usage of `x >= y + 1` or `x - 1 >= y` (and `<=`) in a block
@ -149,19 +149,14 @@ impl IntPlusOne {
} }
fn emit_warning(cx: &EarlyContext<'_>, block: &Expr, recommendation: String) { fn emit_warning(cx: &EarlyContext<'_>, block: &Expr, recommendation: String) {
span_lint_and_then( span_lint_and_sugg(
cx, cx,
INT_PLUS_ONE, INT_PLUS_ONE,
block.span, block.span,
"Unnecessary `>= y + 1` or `x - 1 >=`", "Unnecessary `>= y + 1` or `x - 1 >=`",
|diag| { "change it to",
diag.span_suggestion( recommendation,
block.span, Applicability::MachineApplicable, // snippet
"change it to",
recommendation,
Applicability::MachineApplicable, // snippet
);
},
); );
} }
} }

View File

@ -2471,45 +2471,50 @@ fn check_needless_collect<'a, 'tcx>(expr: &'tcx Expr<'_>, cx: &LateContext<'a, '
match_type(cx, ty, &paths::HASHMAP) { match_type(cx, ty, &paths::HASHMAP) {
if method.ident.name == sym!(len) { if method.ident.name == sym!(len) {
let span = shorten_needless_collect_span(expr); let span = shorten_needless_collect_span(expr);
span_lint_and_then(cx, NEEDLESS_COLLECT, span, NEEDLESS_COLLECT_MSG, |diag| { span_lint_and_sugg(cx,
diag.span_suggestion( NEEDLESS_COLLECT,
span, span,
NEEDLESS_COLLECT_MSG,
"replace with", "replace with",
".count()".to_string(), ".count()".to_string(),
Applicability::MachineApplicable, Applicability::MachineApplicable,
); );
});
} }
if method.ident.name == sym!(is_empty) { if method.ident.name == sym!(is_empty) {
let span = shorten_needless_collect_span(expr); let span = shorten_needless_collect_span(expr);
span_lint_and_then(cx, NEEDLESS_COLLECT, span, NEEDLESS_COLLECT_MSG, |diag| { span_lint_and_sugg(cx,
diag.span_suggestion( NEEDLESS_COLLECT,
span, span,
"replace with", NEEDLESS_COLLECT_MSG,
".next().is_none()".to_string(), "replace with",
Applicability::MachineApplicable, ".next().is_none()".to_string(),
Applicability::MachineApplicable,
); );
});
} }
if method.ident.name == sym!(contains) { if method.ident.name == sym!(contains) {
let contains_arg = snippet(cx, args[1].span, "??"); let contains_arg = snippet(cx, args[1].span, "??");
let span = shorten_needless_collect_span(expr); let span = shorten_needless_collect_span(expr);
span_lint_and_then(cx, NEEDLESS_COLLECT, span, NEEDLESS_COLLECT_MSG, |diag| { span_lint_and_then(cx,
let (arg, pred) = if contains_arg.starts_with('&') { NEEDLESS_COLLECT,
("x", &contains_arg[1..]) span,
} else { NEEDLESS_COLLECT_MSG,
("&x", &*contains_arg) |db| {
}; let (arg, pred) = if contains_arg.starts_with('&') {
diag.span_suggestion( ("x", &contains_arg[1..])
span, } else {
"replace with", ("&x", &*contains_arg)
format!( };
".any(|{}| x == {})", db.span_suggestion(
arg, pred span,
), "replace with",
Applicability::MachineApplicable, format!(
); ".any(|{}| x == {})",
}); arg, pred
),
Applicability::MachineApplicable,
);
}
);
} }
} }
} }

View File

@ -2,8 +2,8 @@
use crate::utils::ptr::get_spans; use crate::utils::ptr::get_spans;
use crate::utils::{ use crate::utils::{
is_type_diagnostic_item, match_qpath, match_type, paths, snippet_opt, span_lint, span_lint_and_then, is_type_diagnostic_item, match_qpath, match_type, paths, snippet_opt, span_lint, span_lint_and_sugg,
walk_ptrs_hir_ty, span_lint_and_then, walk_ptrs_hir_ty,
}; };
use if_chain::if_chain; use if_chain::if_chain;
use rustc_errors::Applicability; use rustc_errors::Applicability;
@ -234,19 +234,14 @@ fn check_fn(cx: &LateContext<'_, '_>, decl: &FnDecl<'_>, fn_id: HirId, opt_body_
then { then {
let replacement = snippet_opt(cx, inner.span); let replacement = snippet_opt(cx, inner.span);
if let Some(r) = replacement { if let Some(r) = replacement {
span_lint_and_then( span_lint_and_sugg(
cx, cx,
PTR_ARG, PTR_ARG,
arg.span, arg.span,
"using a reference to `Cow` is not recommended.", "using a reference to `Cow` is not recommended.",
|diag| { "change this to",
diag.span_suggestion( "&".to_owned() + &r,
arg.span, Applicability::Unspecified,
"change this to",
"&".to_owned() + &r,
Applicability::Unspecified,
);
},
); );
} }
} }

View File

@ -8,7 +8,7 @@ use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::source_map::Span; use rustc_span::source_map::Span;
use rustc_span::BytePos; 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! { declare_clippy_lint! {
/// **What it does:** Checks for return statements at the end of a block. /// **What it does:** Checks for return statements at the end of a block.
@ -162,24 +162,26 @@ impl Return {
}, },
None => match replacement { None => match replacement {
RetReplacement::Empty => { RetReplacement::Empty => {
span_lint_and_then(cx, NEEDLESS_RETURN, ret_span, "unneeded `return` statement", |diag| { span_lint_and_sugg(
diag.span_suggestion( cx,
ret_span, NEEDLESS_RETURN,
"remove `return`", ret_span,
String::new(), "unneeded `return` statement",
Applicability::MachineApplicable, "remove `return`",
); String::new(),
}); Applicability::MachineApplicable,
);
}, },
RetReplacement::Block => { RetReplacement::Block => {
span_lint_and_then(cx, NEEDLESS_RETURN, ret_span, "unneeded `return` statement", |diag| { span_lint_and_sugg(
diag.span_suggestion( cx,
ret_span, NEEDLESS_RETURN,
"replace `return` with an empty block", ret_span,
"{}".to_string(), "unneeded `return` statement",
Applicability::MachineApplicable, "replace `return` with an empty block",
); "{}".to_string(),
}); Applicability::MachineApplicable,
);
}, },
}, },
} }
@ -259,14 +261,15 @@ impl EarlyLintPass for Return {
} else { } else {
(ty.span, Applicability::MaybeIncorrect) (ty.span, Applicability::MaybeIncorrect)
}; };
span_lint_and_then(cx, UNUSED_UNIT, rspan, "unneeded unit return type", |diag| { span_lint_and_sugg(
diag.span_suggestion( cx,
rspan, UNUSED_UNIT,
"remove the `-> ()`", rspan,
String::new(), "unneeded unit return type",
appl, "remove the `-> ()`",
); String::new(),
}); appl,
);
} }
} }
} }
@ -279,14 +282,16 @@ impl EarlyLintPass for Return {
if is_unit_expr(expr) && !stmt.span.from_expansion(); if is_unit_expr(expr) && !stmt.span.from_expansion();
then { then {
let sp = expr.span; let sp = expr.span;
span_lint_and_then(cx, UNUSED_UNIT, sp, "unneeded unit expression", |diag| { span_lint_and_sugg(
diag.span_suggestion( cx,
sp, UNUSED_UNIT,
"remove the final `()`", sp,
String::new(), "unneeded unit expression",
Applicability::MachineApplicable, "remove the final `()`",
); String::new(),
}); Applicability::MachineApplicable,
);
} }
} }
} }
@ -295,14 +300,15 @@ impl EarlyLintPass for Return {
match e.kind { match e.kind {
ast::ExprKind::Ret(Some(ref expr)) | ast::ExprKind::Break(_, Some(ref expr)) => { ast::ExprKind::Ret(Some(ref expr)) | ast::ExprKind::Break(_, Some(ref expr)) => {
if is_unit_expr(expr) && !expr.span.from_expansion() { if is_unit_expr(expr) && !expr.span.from_expansion() {
span_lint_and_then(cx, UNUSED_UNIT, expr.span, "unneeded `()`", |diag| { span_lint_and_sugg(
diag.span_suggestion( cx,
expr.span, UNUSED_UNIT,
"remove the `()`", expr.span,
String::new(), "unneeded `()`",
Applicability::MachineApplicable, "remove the `()`",
); String::new(),
}); Applicability::MachineApplicable,
);
} }
}, },
_ => (), _ => (),

View File

@ -1,5 +1,6 @@
use crate::utils::{ 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 if_chain::if_chain;
use rustc_ast::ast; use rustc_ast::ast;
@ -441,24 +442,19 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Transmute {
"" ""
}; };
span_lint_and_then( span_lint_and_sugg(
cx, cx,
TRANSMUTE_BYTES_TO_STR, TRANSMUTE_BYTES_TO_STR,
e.span, e.span,
&format!("transmute from a `{}` to a `{}`", from_ty, to_ty), &format!("transmute from a `{}` to a `{}`", from_ty, to_ty),
|diag| { "consider using",
diag.span_suggestion( format!(
e.span, "std::str::from_utf8{}({}).unwrap()",
"consider using", postfix,
format!( snippet(cx, args[0].span, ".."),
"std::str::from_utf8{}({}).unwrap()", ),
postfix, Applicability::Unspecified,
snippet(cx, args[0].span, ".."), );
),
Applicability::Unspecified,
);
}
)
} else { } else {
if cx.tcx.erase_regions(&from_ty) != cx.tcx.erase_regions(&to_ty) { if cx.tcx.erase_regions(&from_ty) != cx.tcx.erase_regions(&to_ty) {
span_lint_and_then( span_lint_and_then(