Change note_span argument for span_lint_and_note.

This commit is contained in:
xiongmao86 2020-04-18 18:29:36 +08:00
parent cf4e35339b
commit d7f1a1ed2b
13 changed files with 47 additions and 35 deletions

View file

@ -183,7 +183,7 @@ fn lint_same_then_else(cx: &LateContext<'_, '_>, blocks: &[&Block<'_>]) {
IF_SAME_THEN_ELSE, IF_SAME_THEN_ELSE,
j.span, j.span,
"this `if` has identical blocks", "this `if` has identical blocks",
i.span, Some(i.span),
"same as this", "same as this",
); );
} }
@ -206,7 +206,7 @@ fn lint_same_cond(cx: &LateContext<'_, '_>, conds: &[&Expr<'_>]) {
IFS_SAME_COND, IFS_SAME_COND,
j.span, j.span,
"this `if` has the same condition as a previous `if`", "this `if` has the same condition as a previous `if`",
i.span, Some(i.span),
"same as this", "same as this",
); );
} }
@ -234,7 +234,7 @@ fn lint_same_fns_in_if_cond(cx: &LateContext<'_, '_>, conds: &[&Expr<'_>]) {
SAME_FUNCTIONS_IN_IF_CONDITION, SAME_FUNCTIONS_IN_IF_CONDITION,
j.span, j.span,
"this `if` has the same function call as a previous `if`", "this `if` has the same function call as a previous `if`",
i.span, Some(i.span),
"same as this", "same as this",
); );
} }

View file

@ -46,7 +46,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for CopyIterator {
COPY_ITERATOR, COPY_ITERATOR,
item.span, item.span,
"you are implementing `Iterator` on a `Copy` type", "you are implementing `Iterator` on a `Copy` type",
item.span, None,
"consider implementing `IntoIterator` instead", "consider implementing `IntoIterator` instead",
); );
} }

View file

@ -168,7 +168,7 @@ fn check_copy_clone<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, item: &Item<'_>, trait
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",
item.span, Some(item.span),
"consider deriving `Clone` or removing `Copy`", "consider deriving `Clone` or removing `Copy`",
); );
} }

View file

@ -135,7 +135,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for DropForgetRef {
lint, lint,
expr.span, expr.span,
&msg, &msg,
arg.span, Some(arg.span),
&format!("argument has type `{}`", arg_ty)); &format!("argument has type `{}`", arg_ty));
} else if is_copy(cx, arg_ty) { } else if is_copy(cx, arg_ty) {
if match_def_path(cx, def_id, &paths::DROP) { if match_def_path(cx, def_id, &paths::DROP) {
@ -151,7 +151,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for DropForgetRef {
lint, lint,
expr.span, expr.span,
&msg, &msg,
arg.span, Some(arg.span),
&format!("argument has type {}", arg_ty)); &format!("argument has type {}", arg_ty));
} }
} }

View file

@ -310,7 +310,7 @@ impl<'a, 'tcx> Visitor<'tcx> for ReadVisitor<'a, 'tcx> {
EVAL_ORDER_DEPENDENCE, EVAL_ORDER_DEPENDENCE,
expr.span, expr.span,
"unsequenced read of a variable", "unsequenced read of a variable",
self.write_expr.span, Some(self.write_expr.span),
"whether read occurs before this write depends on evaluation order" "whether read occurs before this write depends on evaluation order"
); );
} }

View file

@ -149,7 +149,7 @@ fn check_assign(cx: &EarlyContext<'_>, expr: &Expr) {
really are doing `.. = ({op} ..)`", really are doing `.. = ({op} ..)`",
op = op op = op
), ),
eqop_span, None,
&format!("to remove this lint, use either `{op}=` or `= {op}`", op = op), &format!("to remove this lint, use either `{op}=` or `= {op}`", op = op),
); );
} }
@ -227,7 +227,7 @@ fn check_else(cx: &EarlyContext<'_>, expr: &Expr) {
SUSPICIOUS_ELSE_FORMATTING, SUSPICIOUS_ELSE_FORMATTING,
else_span, else_span,
&format!("this is an `else {}` but the formatting might hide it", else_desc), &format!("this is an `else {}` but the formatting might hide it", else_desc),
else_span, None,
&format!( &format!(
"to remove this lint, remove the `else` or remove the new line between \ "to remove this lint, remove the `else` or remove the new line between \
`else` and `{}`", `else` and `{}`",
@ -266,7 +266,7 @@ fn check_array(cx: &EarlyContext<'_>, expr: &Expr) {
POSSIBLE_MISSING_COMMA, POSSIBLE_MISSING_COMMA,
lint_span, lint_span,
"possibly missing a comma here", "possibly missing a comma here",
lint_span, None,
"to remove this lint, add a comma or write the expr in a single line", "to remove this lint, add a comma or write the expr in a single line",
); );
} }
@ -297,7 +297,7 @@ fn check_missing_else(cx: &EarlyContext<'_>, first: &Expr, second: &Expr) {
SUSPICIOUS_ELSE_FORMATTING, SUSPICIOUS_ELSE_FORMATTING,
else_span, else_span,
&format!("this looks like {} but the `else` is missing", looks_like), &format!("this looks like {} but the `else` is missing", looks_like),
else_span, None,
&format!( &format!(
"to remove this lint, add the missing `else` or add a new line before {}", "to remove this lint, add the missing `else` or add a new line before {}",
next_thing, next_thing,

View file

@ -637,7 +637,7 @@ fn check_overlapping_arms<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, ex: &'tcx Expr<'
MATCH_OVERLAPPING_ARM, MATCH_OVERLAPPING_ARM,
start.span, start.span,
"some ranges overlap", "some ranges overlap",
end.span, Some(end.span),
"overlaps with this", "overlaps with this",
); );
} }
@ -675,7 +675,7 @@ fn check_wild_err_arm(cx: &LateContext<'_, '_>, ex: &Expr<'_>, arms: &[Arm<'_>])
MATCH_WILD_ERR_ARM, MATCH_WILD_ERR_ARM,
arm.pat.span, arm.pat.span,
&format!("`Err({})` matches all errors", &ident_bind_name), &format!("`Err({})` matches all errors", &ident_bind_name),
arm.pat.span, None,
"match each error separately or use the error output", "match each error separately or use the error output",
); );
} }

View file

@ -2577,7 +2577,7 @@ fn lint_map_unwrap_or_else<'a, 'tcx>(
}, },
expr.span, expr.span,
msg, msg,
expr.span, None,
&format!( &format!(
"replace `map({0}).unwrap_or_else({1})` with `map_or_else({1}, {0})`", "replace `map({0}).unwrap_or_else({1})` with `map_or_else({1}, {0})`",
map_snippet, unwrap_snippet, map_snippet, unwrap_snippet,
@ -2757,7 +2757,7 @@ fn lint_filter_next<'a, 'tcx>(
FILTER_NEXT, FILTER_NEXT,
expr.span, expr.span,
msg, msg,
expr.span, None,
&format!("replace `filter({0}).next()` with `find({0})`", filter_snippet), &format!("replace `filter({0}).next()` with `find({0})`", filter_snippet),
); );
} else { } else {
@ -2816,7 +2816,7 @@ fn lint_filter_map_next<'a, 'tcx>(
FILTER_MAP_NEXT, FILTER_MAP_NEXT,
expr.span, expr.span,
msg, msg,
expr.span, None,
&format!("replace `filter_map({0}).next()` with `find_map({0})`", filter_snippet), &format!("replace `filter_map({0}).next()` with `find_map({0})`", filter_snippet),
); );
} else { } else {

View file

@ -108,15 +108,15 @@ pub fn span_lint_and_note<'a, T: LintContext>(
lint: &'static Lint, lint: &'static Lint,
span: Span, span: Span,
msg: &str, msg: &str,
note_span: Span, note_span: Option<Span>,
note: &str, note: &str,
) { ) {
cx.struct_span_lint(lint, span, |diag| { cx.struct_span_lint(lint, span, |ldb| {
let mut diag = diag.build(msg); let mut db = ldb.build(msg);
if note_span == span { if let Some(note_span) = note_span {
diag.note(note); db.span_note(note_span, note);
} else { } else {
diag.span_note(note_span, note); db.note(note);
} }
docs_link(&mut diag, lint); docs_link(&mut diag, lint);
diag.emit(); diag.emit();

View file

@ -198,8 +198,8 @@ declare_clippy_lint! {
/// ); /// );
/// span_lint_and_help(cx, TEST_LINT, expr.span, lint_msg, Some(expr.span), help_msg); /// span_lint_and_help(cx, TEST_LINT, expr.span, lint_msg, Some(expr.span), help_msg);
/// span_lint_and_help(cx, TEST_LINT, expr.span, lint_msg, None, help_msg); /// span_lint_and_help(cx, TEST_LINT, expr.span, lint_msg, None, help_msg);
/// span_lint_and_note(cx, TEST_LINT, expr.span, lint_msg, expr.span, note_msg); /// span_lint_and_note(cx, TEST_LINT, expr.span, lint_msg, Some(expr.span), note_msg);
/// span_lint_and_note(cx, TEST_LINT, expr.span, lint_msg, expr.span, note_msg); /// span_lint_and_note(cx, TEST_LINT, expr.span, lint_msg, None, note_msg);
/// ``` /// ```
pub COLLAPSIBLE_SPAN_LINT_CALLS, pub COLLAPSIBLE_SPAN_LINT_CALLS,
internal, internal,
@ -486,7 +486,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for CollapsibleCalls {
}, },
"span_note" if sle.eq_expr(&and_then_args[2], &span_call_args[1]) => { "span_note" if sle.eq_expr(&and_then_args[2], &span_call_args[1]) => {
let note_snippet = snippet(cx, span_call_args[2].span, r#""...""#); let note_snippet = snippet(cx, span_call_args[2].span, r#""...""#);
suggest_note(cx, expr, &and_then_snippets, note_snippet.borrow()); suggest_note(cx, expr, &and_then_snippets, note_snippet.borrow(), true);
}, },
"help" => { "help" => {
let help_snippet = snippet(cx, span_call_args[1].span, r#""...""#); let help_snippet = snippet(cx, span_call_args[1].span, r#""...""#);
@ -494,7 +494,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for CollapsibleCalls {
} }
"note" => { "note" => {
let note_snippet = snippet(cx, span_call_args[1].span, r#""...""#); let note_snippet = snippet(cx, span_call_args[1].span, r#""...""#);
suggest_note(cx, expr, &and_then_snippets, note_snippet.borrow()); suggest_note(cx, expr, &and_then_snippets, note_snippet.borrow(), false);
} }
_ => (), _ => (),
} }
@ -606,7 +606,19 @@ fn suggest_help(
); );
} }
fn suggest_note(cx: &LateContext<'_, '_>, expr: &Expr<'_>, and_then_snippets: &AndThenSnippets<'_>, note: &str) { fn suggest_note(
cx: &LateContext<'_, '_>,
expr: &Expr<'_>,
and_then_snippets: &AndThenSnippets<'_>,
note: &str,
with_span: bool,
) {
let note_span = if with_span {
format!("Some({})", and_then_snippets.span)
} else {
"None".to_string()
};
span_lint_and_sugg( span_lint_and_sugg(
cx, cx,
COLLAPSIBLE_SPAN_LINT_CALLS, COLLAPSIBLE_SPAN_LINT_CALLS,
@ -619,7 +631,7 @@ fn suggest_note(cx: &LateContext<'_, '_>, expr: &Expr<'_>, and_then_snippets: &A
and_then_snippets.lint, and_then_snippets.lint,
and_then_snippets.span, and_then_snippets.span,
and_then_snippets.msg, and_then_snippets.msg,
and_then_snippets.span, note_span,
note note
), ),
Applicability::MachineApplicable, Applicability::MachineApplicable,

View file

@ -38,7 +38,7 @@ fn span_lint_and_note<'a, T: LintContext>(
lint: &'static Lint, lint: &'static Lint,
span: Span, span: Span,
msg: &str, msg: &str,
note_span: Span, note_span: Option<Span>,
note: &str, note: &str,
) { ) {
} }
@ -75,8 +75,8 @@ impl EarlyLintPass for Pass {
span_lint_and_sugg(cx, TEST_LINT, expr.span, lint_msg, help_msg, sugg.to_string(), Applicability::MachineApplicable); span_lint_and_sugg(cx, TEST_LINT, expr.span, lint_msg, help_msg, sugg.to_string(), Applicability::MachineApplicable);
span_lint_and_help(cx, TEST_LINT, expr.span, lint_msg, Some(expr.span), help_msg); span_lint_and_help(cx, TEST_LINT, expr.span, lint_msg, Some(expr.span), help_msg);
span_lint_and_help(cx, TEST_LINT, expr.span, lint_msg, None, help_msg); span_lint_and_help(cx, TEST_LINT, expr.span, lint_msg, None, help_msg);
span_lint_and_note(cx, TEST_LINT, expr.span, lint_msg, expr.span, note_msg); span_lint_and_note(cx, TEST_LINT, expr.span, lint_msg, Some(expr.span), note_msg);
span_lint_and_note(cx, TEST_LINT, expr.span, lint_msg, expr.span, note_msg); span_lint_and_note(cx, TEST_LINT, expr.span, lint_msg, None, note_msg);
// This expr shouldn't trigger this lint. // This expr shouldn't trigger this lint.
span_lint_and_then(cx, TEST_LINT, expr.span, lint_msg, |db| { span_lint_and_then(cx, TEST_LINT, expr.span, lint_msg, |db| {

View file

@ -38,7 +38,7 @@ fn span_lint_and_note<'a, T: LintContext>(
lint: &'static Lint, lint: &'static Lint,
span: Span, span: Span,
msg: &str, msg: &str,
note_span: Span, note_span: Option<Span>,
note: &str, note: &str,
) { ) {
} }

View file

@ -35,7 +35,7 @@ error: this call is collspible
LL | / span_lint_and_then(cx, TEST_LINT, expr.span, lint_msg, |db| { LL | / span_lint_and_then(cx, TEST_LINT, expr.span, lint_msg, |db| {
LL | | db.span_note(expr.span, note_msg); LL | | db.span_note(expr.span, note_msg);
LL | | }); LL | | });
| |__________^ help: collapse into: `span_lint_and_note(cx, TEST_LINT, expr.span, lint_msg, expr.span, note_msg)` | |__________^ help: collapse into: `span_lint_and_note(cx, TEST_LINT, expr.span, lint_msg, Some(expr.span), note_msg)`
error: this call is collspible error: this call is collspible
--> $DIR/collapsible_span_lint_calls.rs:87:9 --> $DIR/collapsible_span_lint_calls.rs:87:9
@ -43,7 +43,7 @@ error: this call is collspible
LL | / span_lint_and_then(cx, TEST_LINT, expr.span, lint_msg, |db| { LL | / span_lint_and_then(cx, TEST_LINT, expr.span, lint_msg, |db| {
LL | | db.note(note_msg); LL | | db.note(note_msg);
LL | | }); LL | | });
| |__________^ help: collapse into: `span_lint_and_note(cx, TEST_LINT, expr.span, lint_msg, expr.span, note_msg)` | |__________^ help: collapse into: `span_lint_and_note(cx, TEST_LINT, expr.span, lint_msg, None, note_msg)`
error: aborting due to 5 previous errors error: aborting due to 5 previous errors