Skip to content

Commit 68bb0e8

Browse files
committed
Migrating restriction lints to span_lint_and_then (e -> i 40/116)
1 parent 2f06fb6 commit 68bb0e8

19 files changed

+667
-204
lines changed
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;
@@ -57,13 +57,15 @@ impl EarlyLintPass for ElseIfWithoutElse {
5757
if let ExprKind::If(_, _, Some(ref els)) = item.kind
5858
&& let ExprKind::If(_, _, None) = els.kind
5959
{
60-
span_lint_and_help(
60+
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
61+
span_lint_and_then(
6162
cx,
6263
ELSE_IF_WITHOUT_ELSE,
6364
els.span,
6465
"`if` expression with an `else if`, but without a final `else`",
65-
None,
66-
"add an `else` block here",
66+
|diag| {
67+
diag.help("add an `else` block here");
68+
},
6769
);
6870
}
6971
}

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

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::source::indent_of;
3-
use rustc_errors::Applicability;
3+
use rustc_errors::{Applicability, SuggestionStyle};
44
use rustc_hir::{Item, ItemKind};
55
use rustc_lint::{LateContext, LateLintPass};
66
use rustc_session::declare_lint_pass;
@@ -84,15 +84,16 @@ impl LateLintPass<'_> for ExhaustiveItems {
8484
} else {
8585
(EXHAUSTIVE_ENUMS, "exported enums should not be exhaustive")
8686
};
87-
let suggestion_span = item.span.shrink_to_lo();
88-
let indent = " ".repeat(indent_of(cx, item.span).unwrap_or(0));
8987
span_lint_and_then(cx, lint, item.span, msg, |diag| {
88+
let suggestion_span = item.span.shrink_to_lo();
89+
let indent = " ".repeat(indent_of(cx, item.span).unwrap_or(0));
9090
let sugg = format!("#[non_exhaustive]\n{indent}");
91-
diag.span_suggestion(
91+
diag.span_suggestion_with_style(
9292
suggestion_span,
9393
"try adding #[non_exhaustive]",
9494
sugg,
9595
Applicability::MaybeIncorrect,
96+
SuggestionStyle::ShowAlways,
9697
);
9798
});
9899
}

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;
@@ -96,32 +96,39 @@ impl<'tcx> LateLintPass<'tcx> for IfThenSomeElseNone {
9696
&& is_res_lang_ctor(cx, path_res(cx, peel_blocks(els)), OptionNone)
9797
&& !contains_return(then_block.stmts)
9898
{
99-
let mut app = Applicability::Unspecified;
100-
let cond_snip = Sugg::hir_with_context(cx, cond, expr.span.ctxt(), "[condition]", &mut app)
101-
.maybe_par()
102-
.to_string();
103-
let arg_snip = snippet_with_context(cx, then_arg.span, ctxt, "[body]", &mut app).0;
104-
let mut method_body = if then_block.stmts.is_empty() {
105-
arg_snip.into_owned()
106-
} else {
107-
format!("{{ /* snippet */ {arg_snip} }}")
108-
};
10999
let method_name = if switch_to_eager_eval(cx, expr) && self.msrv.meets(msrvs::BOOL_THEN_SOME) {
110100
"then_some"
111101
} else {
112-
method_body.insert_str(0, "|| ");
113102
"then"
114103
};
115104

116-
let help =
117-
format!("consider using `bool::{method_name}` like: `{cond_snip}.{method_name}({method_body})`",);
118-
span_lint_and_help(
105+
span_lint_and_then(
119106
cx,
120107
IF_THEN_SOME_ELSE_NONE,
121108
expr.span,
122109
format!("this could be simplified with `bool::{method_name}`"),
123-
None,
124-
help,
110+
|diag| {
111+
let mut app = Applicability::Unspecified;
112+
let cond_snip = Sugg::hir_with_context(cx, cond, expr.span.ctxt(), "[condition]", &mut app)
113+
.maybe_par()
114+
.to_string();
115+
let arg_snip = snippet_with_context(cx, then_arg.span, ctxt, "[body]", &mut app).0;
116+
let method_body = if let Some(first_stmt) = then_block.stmts.first() {
117+
let (block_snippet, _) =
118+
snippet_with_context(cx, first_stmt.span.until(then_arg.span), ctxt, "..", &mut app);
119+
let closure = if method_name == "then" { "|| " } else { "" };
120+
format!("{closure} {{ {block_snippet}; {arg_snip} }}")
121+
} else {
122+
arg_snip.into_owned()
123+
};
124+
125+
diag.span_suggestion(
126+
expr.span,
127+
"try",
128+
format!("{cond_snip}.{method_name}({method_body})"),
129+
app,
130+
);
131+
},
125132
);
126133
}
127134
}

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)