Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fixes internal lint warning in code base.
  • Loading branch information
xiongmao86 committed Apr 18, 2020
commit d03d3bd95b06b82246a51aaa8e424a67eb724037
9 changes: 4 additions & 5 deletions clippy_lints/src/derive.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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`",
);
}
}
17 changes: 9 additions & 8 deletions clippy_lints/src/empty_enum.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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",
);
}
}
}
Expand Down
20 changes: 11 additions & 9 deletions clippy_lints/src/eta_reduction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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! {
Expand Down Expand Up @@ -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,
);
}
);
}
Expand Down
53 changes: 28 additions & 25 deletions clippy_lints/src/identity_conversion.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -58,29 +58,31 @@ 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" {
let a = cx.tables.expr_ty(e);
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
);
}
}
},
Expand All @@ -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
);
}
}
}
Expand Down
15 changes: 5 additions & 10 deletions clippy_lints/src/int_plus_one.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
);
}
}
Expand Down
59 changes: 32 additions & 27 deletions clippy_lints/src/loops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
}
);
}
}
}
Expand Down
17 changes: 6 additions & 11 deletions clippy_lints/src/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
);
}
}
Expand Down
Loading