Skip to content

Commit

Permalink
Change note_span argument for span_lint_and_note.
Browse files Browse the repository at this point in the history
  • Loading branch information
xiongmao86 committed Apr 18, 2020
1 parent cf4e353 commit d7f1a1e
Show file tree
Hide file tree
Showing 13 changed files with 47 additions and 35 deletions.
6 changes: 3 additions & 3 deletions clippy_lints/src/copies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ fn lint_same_then_else(cx: &LateContext<'_, '_>, blocks: &[&Block<'_>]) {
IF_SAME_THEN_ELSE,
j.span,
"this `if` has identical blocks",
i.span,
Some(i.span),
"same as this",
);
}
Expand All @@ -206,7 +206,7 @@ fn lint_same_cond(cx: &LateContext<'_, '_>, conds: &[&Expr<'_>]) {
IFS_SAME_COND,
j.span,
"this `if` has the same condition as a previous `if`",
i.span,
Some(i.span),
"same as this",
);
}
Expand Down Expand Up @@ -234,7 +234,7 @@ fn lint_same_fns_in_if_cond(cx: &LateContext<'_, '_>, conds: &[&Expr<'_>]) {
SAME_FUNCTIONS_IN_IF_CONDITION,
j.span,
"this `if` has the same function call as a previous `if`",
i.span,
Some(i.span),
"same as this",
);
}
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/copy_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for CopyIterator {
COPY_ITERATOR,
item.span,
"you are implementing `Iterator` on a `Copy` type",
item.span,
None,
"consider implementing `IntoIterator` instead",
);
}
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ fn check_copy_clone<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, item: &Item<'_>, trait
EXPL_IMPL_CLONE_ON_COPY,
item.span,
"you are implementing `Clone` explicitly on a `Copy` type",
item.span,
Some(item.span),
"consider deriving `Clone` or removing `Copy`",
);
}
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/drop_forget_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for DropForgetRef {
lint,
expr.span,
&msg,
arg.span,
Some(arg.span),
&format!("argument has type `{}`", arg_ty));
} else if is_copy(cx, arg_ty) {
if match_def_path(cx, def_id, &paths::DROP) {
Expand All @@ -151,7 +151,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for DropForgetRef {
lint,
expr.span,
&msg,
arg.span,
Some(arg.span),
&format!("argument has type {}", arg_ty));
}
}
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/eval_order_dependence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ impl<'a, 'tcx> Visitor<'tcx> for ReadVisitor<'a, 'tcx> {
EVAL_ORDER_DEPENDENCE,
expr.span,
"unsequenced read of a variable",
self.write_expr.span,
Some(self.write_expr.span),
"whether read occurs before this write depends on evaluation order"
);
}
Expand Down
8 changes: 4 additions & 4 deletions clippy_lints/src/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ fn check_assign(cx: &EarlyContext<'_>, expr: &Expr) {
really are doing `.. = ({op} ..)`",
op = op
),
eqop_span,
None,
&format!("to remove this lint, use either `{op}=` or `= {op}`", op = op),
);
}
Expand Down Expand Up @@ -227,7 +227,7 @@ fn check_else(cx: &EarlyContext<'_>, expr: &Expr) {
SUSPICIOUS_ELSE_FORMATTING,
else_span,
&format!("this is an `else {}` but the formatting might hide it", else_desc),
else_span,
None,
&format!(
"to remove this lint, remove the `else` or remove the new line between \
`else` and `{}`",
Expand Down Expand Up @@ -266,7 +266,7 @@ fn check_array(cx: &EarlyContext<'_>, expr: &Expr) {
POSSIBLE_MISSING_COMMA,
lint_span,
"possibly missing a comma here",
lint_span,
None,
"to remove this lint, add a comma or write the expr in a single line",
);
}
Expand Down Expand Up @@ -297,7 +297,7 @@ fn check_missing_else(cx: &EarlyContext<'_>, first: &Expr, second: &Expr) {
SUSPICIOUS_ELSE_FORMATTING,
else_span,
&format!("this looks like {} but the `else` is missing", looks_like),
else_span,
None,
&format!(
"to remove this lint, add the missing `else` or add a new line before {}",
next_thing,
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/matches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ fn check_overlapping_arms<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, ex: &'tcx Expr<'
MATCH_OVERLAPPING_ARM,
start.span,
"some ranges overlap",
end.span,
Some(end.span),
"overlaps with this",
);
}
Expand Down Expand Up @@ -675,7 +675,7 @@ fn check_wild_err_arm(cx: &LateContext<'_, '_>, ex: &Expr<'_>, arms: &[Arm<'_>])
MATCH_WILD_ERR_ARM,
arm.pat.span,
&format!("`Err({})` matches all errors", &ident_bind_name),
arm.pat.span,
None,
"match each error separately or use the error output",
);
}
Expand Down
6 changes: 3 additions & 3 deletions clippy_lints/src/methods/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2577,7 +2577,7 @@ fn lint_map_unwrap_or_else<'a, 'tcx>(
},
expr.span,
msg,
expr.span,
None,
&format!(
"replace `map({0}).unwrap_or_else({1})` with `map_or_else({1}, {0})`",
map_snippet, unwrap_snippet,
Expand Down Expand Up @@ -2757,7 +2757,7 @@ fn lint_filter_next<'a, 'tcx>(
FILTER_NEXT,
expr.span,
msg,
expr.span,
None,
&format!("replace `filter({0}).next()` with `find({0})`", filter_snippet),
);
} else {
Expand Down Expand Up @@ -2816,7 +2816,7 @@ fn lint_filter_map_next<'a, 'tcx>(
FILTER_MAP_NEXT,
expr.span,
msg,
expr.span,
None,
&format!("replace `filter_map({0}).next()` with `find_map({0})`", filter_snippet),
);
} else {
Expand Down
12 changes: 6 additions & 6 deletions clippy_lints/src/utils/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,15 @@ pub fn span_lint_and_note<'a, T: LintContext>(
lint: &'static Lint,
span: Span,
msg: &str,
note_span: Span,
note_span: Option<Span>,
note: &str,
) {
cx.struct_span_lint(lint, span, |diag| {
let mut diag = diag.build(msg);
if note_span == span {
diag.note(note);
cx.struct_span_lint(lint, span, |ldb| {
let mut db = ldb.build(msg);
if let Some(note_span) = note_span {
db.span_note(note_span, note);
} else {
diag.span_note(note_span, note);
db.note(note);
}
docs_link(&mut diag, lint);
diag.emit();
Expand Down
24 changes: 18 additions & 6 deletions clippy_lints/src/utils/internal_lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, 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, 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, None, note_msg);
/// ```
pub COLLAPSIBLE_SPAN_LINT_CALLS,
internal,
Expand Down Expand Up @@ -486,15 +486,15 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for CollapsibleCalls {
},
"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#""...""#);
suggest_note(cx, expr, &and_then_snippets, note_snippet.borrow());
suggest_note(cx, expr, &and_then_snippets, note_snippet.borrow(), true);
},
"help" => {
let help_snippet = snippet(cx, span_call_args[1].span, r#""...""#);
suggest_help(cx, expr, &and_then_snippets, help_snippet.borrow(), false);
}
"note" => {
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);
}
_ => (),
}
Expand Down Expand Up @@ -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(
cx,
COLLAPSIBLE_SPAN_LINT_CALLS,
Expand All @@ -619,7 +631,7 @@ fn suggest_note(cx: &LateContext<'_, '_>, expr: &Expr<'_>, and_then_snippets: &A
and_then_snippets.lint,
and_then_snippets.span,
and_then_snippets.msg,
and_then_snippets.span,
note_span,
note
),
Applicability::MachineApplicable,
Expand Down
6 changes: 3 additions & 3 deletions tests/ui/collapsible_span_lint_calls.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ fn span_lint_and_note<'a, T: LintContext>(
lint: &'static Lint,
span: Span,
msg: &str,
note_span: Span,
note_span: Option<Span>,
note: &str,
) {
}
Expand Down Expand Up @@ -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_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_note(cx, TEST_LINT, expr.span, lint_msg, 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, Some(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.
span_lint_and_then(cx, TEST_LINT, expr.span, lint_msg, |db| {
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/collapsible_span_lint_calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ fn span_lint_and_note<'a, T: LintContext>(
lint: &'static Lint,
span: Span,
msg: &str,
note_span: Span,
note_span: Option<Span>,
note: &str,
) {
}
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/collapsible_span_lint_calls.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ error: this call is collspible
LL | / span_lint_and_then(cx, TEST_LINT, expr.span, lint_msg, |db| {
LL | | db.span_note(expr.span, note_msg);
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
--> $DIR/collapsible_span_lint_calls.rs:87:9
|
LL | / span_lint_and_then(cx, TEST_LINT, expr.span, lint_msg, |db| {
LL | | db.note(note_msg);
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

0 comments on commit d7f1a1e

Please sign in to comment.