Skip to content

Commit 0532104

Browse files
committed
Migrating restriction lints to span_lint_and_then (e -> i)
1 parent a9d72c7 commit 0532104

20 files changed

+665
-204
lines changed

clippy_lints/src/assertions_on_result_states.rs

-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ impl<'tcx> LateLintPass<'tcx> for AssertionsOnResultStates {
7777
},
7878
_ => return,
7979
};
80-
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
8180
span_lint_and_then(cx, ASSERTIONS_ON_RESULT_STATES, macro_call.span, message, |diag| {
8281
let semicolon = if is_expr_final_block_expr(cx.tcx, e) { ";" } else { "" };
8382
let mut app = Applicability::MachineApplicable;
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use clippy_utils::diagnostics::span_lint_and_sugg;
1+
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::source::snippet_with_applicability;
3-
use rustc_errors::Applicability;
3+
use rustc_errors::{Applicability, SuggestionStyle};
44
use rustc_hir::Expr;
55
use rustc_lint::LateContext;
66
use rustc_middle::ty::{self, Ty};
@@ -14,21 +14,24 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_expr: &Expr<'_>,
1414
_ => { /* continue to checks */ },
1515
}
1616

17-
match cast_from.kind() {
18-
ty::FnDef(..) | ty::FnPtr(_) => {
19-
let mut applicability = Applicability::MaybeIncorrect;
20-
let from_snippet = snippet_with_applicability(cx, cast_expr.span, "..", &mut applicability);
17+
if let ty::FnDef(..) | ty::FnPtr(_) = cast_from.kind() {
18+
let mut applicability = Applicability::MaybeIncorrect;
19+
let from_snippet = snippet_with_applicability(cx, cast_expr.span, "..", &mut applicability);
2120

22-
span_lint_and_sugg(
23-
cx,
24-
FN_TO_NUMERIC_CAST_ANY,
25-
expr.span,
26-
format!("casting function pointer `{from_snippet}` to `{cast_to}`"),
27-
"did you mean to invoke the function?",
28-
format!("{from_snippet}() as {cast_to}"),
29-
applicability,
30-
);
31-
},
32-
_ => {},
21+
span_lint_and_then(
22+
cx,
23+
FN_TO_NUMERIC_CAST_ANY,
24+
expr.span,
25+
format!("casting function pointer `{from_snippet}` to `{cast_to}`"),
26+
|diag| {
27+
diag.span_suggestion_with_style(
28+
expr.span,
29+
"did you mean to invoke the function?",
30+
format!("{from_snippet}() as {cast_to}"),
31+
applicability,
32+
SuggestionStyle::ShowAlways,
33+
);
34+
},
35+
);
3336
}
3437
}

clippy_lints/src/else_if_without_else.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Lint on if expressions with an else if, but without a final else branch.
22
3-
use clippy_utils::diagnostics::span_lint_and_help;
3+
use clippy_utils::diagnostics::span_lint_and_then;
44
use rustc_ast::ast::{Expr, ExprKind};
55
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
66
use rustc_middle::lint::in_external_macro;
@@ -54,13 +54,15 @@ impl EarlyLintPass for ElseIfWithoutElse {
5454
&& let ExprKind::If(_, _, None) = els.kind
5555
&& !in_external_macro(cx.sess(), item.span)
5656
{
57-
span_lint_and_help(
57+
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
58+
span_lint_and_then(
5859
cx,
5960
ELSE_IF_WITHOUT_ELSE,
6061
els.span,
6162
"`if` expression with an `else if`, but without a final `else`",
62-
None,
63-
"add an `else` block here",
63+
|diag| {
64+
diag.help("add an `else` block here");
65+
},
6466
);
6567
}
6668
}

clippy_lints/src/empty_drop.rs

+9-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clippy_utils::diagnostics::span_lint_and_sugg;
1+
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::peel_blocks;
33
use rustc_errors::Applicability;
44
use rustc_hir::{Body, ExprKind, Impl, ImplItemKind, Item, ItemKind, Node};
@@ -50,15 +50,14 @@ impl LateLintPass<'_> for EmptyDrop {
5050
&& block.stmts.is_empty()
5151
&& block.expr.is_none()
5252
{
53-
span_lint_and_sugg(
54-
cx,
55-
EMPTY_DROP,
56-
item.span,
57-
"empty drop implementation",
58-
"try removing this impl",
59-
String::new(),
60-
Applicability::MaybeIncorrect,
61-
);
53+
span_lint_and_then(cx, EMPTY_DROP, item.span, "empty drop implementation", |diag| {
54+
diag.span_suggestion_hidden(
55+
item.span,
56+
"try removing this impl",
57+
String::new(),
58+
Applicability::MaybeIncorrect,
59+
);
60+
});
6261
}
6362
}
6463
}

clippy_lints/src/exhaustive_items.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,11 @@ impl LateLintPass<'_> for ExhaustiveItems {
8888
&& !attrs.iter().any(|a| a.has_name(sym::non_exhaustive))
8989
&& fields.iter().all(|f| cx.tcx.visibility(f.def_id).is_public())
9090
{
91-
let suggestion_span = item.span.shrink_to_lo();
92-
let indent = " ".repeat(indent_of(cx, item.span).unwrap_or(0));
9391
span_lint_and_then(cx, lint, item.span, msg, |diag| {
92+
let suggestion_span = item.span.shrink_to_lo();
93+
let indent = " ".repeat(indent_of(cx, item.span).unwrap_or(0));
9494
let sugg = format!("#[non_exhaustive]\n{indent}");
95-
diag.span_suggestion(
95+
diag.span_suggestion_verbose(
9696
suggestion_span,
9797
"try adding #[non_exhaustive]",
9898
sugg,

clippy_lints/src/field_scoped_visibility_modifiers.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clippy_utils::diagnostics::span_lint_and_help;
1+
use clippy_utils::diagnostics::span_lint_and_then;
22
use rustc_ast::ast::{Item, ItemKind, VisibilityKind};
33
use rustc_lint::{EarlyContext, EarlyLintPass};
44
use rustc_session::declare_lint_pass;
@@ -62,13 +62,15 @@ impl EarlyLintPass for FieldScopedVisibilityModifiers {
6262
// pub(self) is equivalent to not using pub at all, so we ignore it
6363
continue;
6464
}
65-
span_lint_and_help(
65+
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
66+
span_lint_and_then(
6667
cx,
6768
FIELD_SCOPED_VISIBILITY_MODIFIERS,
6869
field.vis.span,
6970
"scoped visibility modifier on a field",
70-
None,
71-
"consider making the field private and adding a scoped visibility method for it",
71+
|diag| {
72+
diag.help("consider making the field private and adding a scoped visibility method for it");
73+
},
7274
);
7375
}
7476
}

clippy_lints/src/format_push_string.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clippy_utils::diagnostics::span_lint_and_help;
1+
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::ty::is_type_lang_item;
33
use clippy_utils::{higher, match_def_path, paths};
44
use rustc_hir::{BinOpKind, Expr, ExprKind, LangItem, MatchSource};
@@ -81,13 +81,15 @@ impl<'tcx> LateLintPass<'tcx> for FormatPushString {
8181
_ => return,
8282
};
8383
if is_format(cx, arg) {
84-
span_lint_and_help(
84+
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
85+
span_lint_and_then(
8586
cx,
8687
FORMAT_PUSH_STRING,
8788
expr.span,
8889
"`format!(..)` appended to existing `String`",
89-
None,
90-
"consider using `write!` to avoid the extra allocation",
90+
|diag| {
91+
diag.help("consider using `write!` to avoid the extra allocation");
92+
},
9193
);
9294
}
9395
}

clippy_lints/src/if_then_some_else_none.rs

+24-17
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_config::msrvs::{self, Msrv};
22
use clippy_config::Conf;
3-
use clippy_utils::diagnostics::span_lint_and_help;
3+
use clippy_utils::diagnostics::span_lint_and_then;
44
use clippy_utils::eager_or_lazy::switch_to_eager_eval;
55
use clippy_utils::source::snippet_with_context;
66
use clippy_utils::sugg::Sugg;
@@ -81,32 +81,39 @@ impl<'tcx> LateLintPass<'tcx> for IfThenSomeElseNone {
8181
&& self.msrv.meets(msrvs::BOOL_THEN)
8282
&& !contains_return(then_block.stmts)
8383
{
84-
let mut app = Applicability::Unspecified;
85-
let cond_snip = Sugg::hir_with_context(cx, cond, expr.span.ctxt(), "[condition]", &mut app)
86-
.maybe_par()
87-
.to_string();
88-
let arg_snip = snippet_with_context(cx, then_arg.span, ctxt, "[body]", &mut app).0;
89-
let mut method_body = if then_block.stmts.is_empty() {
90-
arg_snip.into_owned()
91-
} else {
92-
format!("{{ /* snippet */ {arg_snip} }}")
93-
};
9484
let method_name = if switch_to_eager_eval(cx, expr) && self.msrv.meets(msrvs::BOOL_THEN_SOME) {
9585
"then_some"
9686
} else {
97-
method_body.insert_str(0, "|| ");
9887
"then"
9988
};
10089

101-
let help =
102-
format!("consider using `bool::{method_name}` like: `{cond_snip}.{method_name}({method_body})`",);
103-
span_lint_and_help(
90+
span_lint_and_then(
10491
cx,
10592
IF_THEN_SOME_ELSE_NONE,
10693
expr.span,
10794
format!("this could be simplified with `bool::{method_name}`"),
108-
None,
109-
help,
95+
|diag| {
96+
let mut app = Applicability::Unspecified;
97+
let cond_snip = Sugg::hir_with_context(cx, cond, expr.span.ctxt(), "[condition]", &mut app)
98+
.maybe_par()
99+
.to_string();
100+
let arg_snip = snippet_with_context(cx, then_arg.span, ctxt, "[body]", &mut app).0;
101+
let method_body = if let Some(first_stmt) = then_block.stmts.first() {
102+
let (block_snippet, _) =
103+
snippet_with_context(cx, first_stmt.span.until(then_arg.span), ctxt, "..", &mut app);
104+
let closure = if method_name == "then" { "|| " } else { "" };
105+
format!("{closure} {{ {block_snippet}; {arg_snip} }}")
106+
} else {
107+
arg_snip.into_owned()
108+
};
109+
110+
diag.span_suggestion(
111+
expr.span,
112+
"try",
113+
format!("{cond_snip}.{method_name}({method_body})"),
114+
app,
115+
);
116+
},
110117
);
111118
}
112119
}

clippy_lints/src/implicit_return.rs

+14-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use clippy_utils::source::{snippet_with_applicability, snippet_with_context, wal
33
use clippy_utils::visitors::for_each_expr_without_closures;
44
use clippy_utils::{get_async_fn_body, is_async_fn, is_from_proc_macro};
55
use core::ops::ControlFlow;
6-
use rustc_errors::Applicability;
6+
use rustc_errors::{Applicability, SuggestionStyle};
77
use rustc_hir::intravisit::FnKind;
88
use rustc_hir::{Block, Body, Expr, ExprKind, FnDecl, FnRetTy, HirId};
99
use rustc_lint::{LateContext, LateLintPass, LintContext};
@@ -45,35 +45,42 @@ declare_clippy_lint! {
4545
declare_lint_pass!(ImplicitReturn => [IMPLICIT_RETURN]);
4646

4747
fn lint_return(cx: &LateContext<'_>, emission_place: HirId, span: Span) {
48-
let mut app = Applicability::MachineApplicable;
49-
let snip = snippet_with_applicability(cx, span, "..", &mut app);
5048
span_lint_hir_and_then(
5149
cx,
5250
IMPLICIT_RETURN,
5351
emission_place,
5452
span,
5553
"missing `return` statement",
5654
|diag| {
57-
diag.span_suggestion(span, "add `return` as shown", format!("return {snip}"), app);
55+
let mut app = Applicability::MachineApplicable;
56+
let snip = snippet_with_applicability(cx, span, "..", &mut app);
57+
diag.span_suggestion_with_style(
58+
span,
59+
"add `return` as shown",
60+
format!("return {snip}"),
61+
app,
62+
SuggestionStyle::ShowAlways,
63+
);
5864
},
5965
);
6066
}
6167

6268
fn lint_break(cx: &LateContext<'_>, emission_place: HirId, break_span: Span, expr_span: Span) {
63-
let mut app = Applicability::MachineApplicable;
64-
let snip = snippet_with_context(cx, expr_span, break_span.ctxt(), "..", &mut app).0;
6569
span_lint_hir_and_then(
6670
cx,
6771
IMPLICIT_RETURN,
6872
emission_place,
6973
break_span,
7074
"missing `return` statement",
7175
|diag| {
72-
diag.span_suggestion(
76+
let mut app = Applicability::MachineApplicable;
77+
let snip = snippet_with_context(cx, expr_span, break_span.ctxt(), "..", &mut app).0;
78+
diag.span_suggestion_with_style(
7379
break_span,
7480
"change `break` to `return` as shown",
7581
format!("return {snip}"),
7682
app,
83+
SuggestionStyle::ShowAlways,
7784
);
7885
},
7986
);

clippy_lints/src/literal_representation.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ impl WarningType {
186186
}
187187
}
188188

189-
fn display(&self, num_lit: NumericLiteral<'_>, cx: &EarlyContext<'_>, span: Span) {
189+
fn display(&self, num_lit: &NumericLiteral<'_>, cx: &EarlyContext<'_>, span: Span) {
190190
let (lint, message, try_msg) = self.lint_and_text();
191191
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
192192
span_lint_and_then(cx, lint, span, message, |diag| {
@@ -269,7 +269,7 @@ impl LiteralDigitGrouping {
269269
WarningType::DecimalRepresentation | WarningType::MistypedLiteralSuffix => true,
270270
};
271271
if should_warn {
272-
warning_type.display(num_lit, cx, span);
272+
warning_type.display(&num_lit, cx, span);
273273
}
274274
}
275275
}
@@ -450,7 +450,7 @@ impl DecimalLiteralRepresentation {
450450
let hex = format!("{val:#X}");
451451
let num_lit = NumericLiteral::new(&hex, num_lit.suffix, false);
452452
let _: Result<(), ()> = Self::do_lint(num_lit.integer).map_err(|warning_type| {
453-
warning_type.display(num_lit, cx, span);
453+
warning_type.display(&num_lit, cx, span);
454454
});
455455
}
456456
}

clippy_lints/src/methods/filetype_is_file.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clippy_utils::diagnostics::span_lint_and_help;
1+
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::get_parent_expr;
33
use clippy_utils::ty::is_type_diagnostic_item;
44
use rustc_hir as hir;
@@ -33,6 +33,9 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &hir::Expr
3333
span = expr.span;
3434
}
3535
let lint_msg = format!("`{lint_unary}FileType::is_file()` only {verb} regular files");
36-
let help_msg = format!("use `{help_unary}FileType::is_dir()` instead");
37-
span_lint_and_help(cx, FILETYPE_IS_FILE, span, lint_msg, None, help_msg);
36+
37+
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
38+
span_lint_and_then(cx, FILETYPE_IS_FILE, span, lint_msg, |diag| {
39+
diag.help(format!("use `{help_unary}FileType::is_dir()` instead"));
40+
});
3841
}

0 commit comments

Comments
 (0)