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::{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`",
);
}
}

View File

@ -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 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EmptyEnum {
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",
);
}
}
}

View File

@ -7,7 +7,8 @@ use rustc_middle::ty::{self, Ty};
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,
);
}
);
}

View File

@ -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 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IdentityConversion {
if same_tys(cx, a, b) {
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| {
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 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IdentityConversion {
let b = cx.tables.expr_ty(&args[0]);
if same_tys(cx, a, b) {
let sugg = snippet(cx, args[0].span, "<expr>").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 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IdentityConversion {
let sugg = snippet(cx, args[0].span.source_callsite(), "<expr>").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
);
}
}
}

View File

@ -5,7 +5,7 @@ use rustc_errors::Applicability;
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 @@ impl IntPlusOne {
}
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
);
}
}

View File

@ -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,
);
}
);
}
}
}

View File

@ -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,
);
}
}

View File

@ -8,7 +8,7 @@ use rustc_session::{declare_lint_pass, declare_tool_lint};
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 @@ impl Return {
},
None => 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 @@ impl EarlyLintPass for Return {
} 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 @@ impl EarlyLintPass for Return {
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 @@ impl EarlyLintPass for Return {
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,
);
}
},
_ => (),

View File

@ -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 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Transmute {
""
};
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(