Skip to content

Commit 884a837

Browse files
committed
Make restriction lint's use span_lint_and_then (q -> s)
1 parent 1807580 commit 884a837

13 files changed

+181
-116
lines changed

clippy_lints/src/matches/rest_pat_in_fully_bound_struct.rs

Lines changed: 6 additions & 4 deletions
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_hir::{Pat, PatKind, QPath};
33
use rustc_lint::LateContext;
44
use rustc_middle::ty;
@@ -15,13 +15,15 @@ pub(crate) fn check(cx: &LateContext<'_>, pat: &Pat<'_>) {
1515
&& fields.len() == def.non_enum_variant().fields.len()
1616
&& !def.non_enum_variant().is_field_list_non_exhaustive()
1717
{
18-
span_lint_and_help(
18+
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
19+
span_lint_and_then(
1920
cx,
2021
REST_PAT_IN_FULLY_BOUND_STRUCTS,
2122
pat.span,
2223
"unnecessary use of `..` pattern in struct binding. All fields were already bound",
23-
None,
24-
"consider removing `..` from this binding",
24+
|diag| {
25+
diag.help("consider removing `..` from this binding");
26+
},
2527
);
2628
}
2729
}

clippy_lints/src/misc_early/literal_suffix.rs

Lines changed: 21 additions & 9 deletions
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 rustc_errors::Applicability;
33
use rustc_lint::EarlyContext;
44
use rustc_span::Span;
@@ -12,24 +12,36 @@ pub(super) fn check(cx: &EarlyContext<'_>, lit_span: Span, lit_snip: &str, suffi
1212
// Do not lint when literal is unsuffixed.
1313
if !suffix.is_empty() {
1414
if lit_snip.as_bytes()[maybe_last_sep_idx] == b'_' {
15-
span_lint_and_sugg(
15+
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
16+
span_lint_and_then(
1617
cx,
1718
SEPARATED_LITERAL_SUFFIX,
1819
lit_span,
1920
format!("{sugg_type} type suffix should not be separated by an underscore"),
20-
"remove the underscore",
21-
format!("{}{suffix}", &lit_snip[..maybe_last_sep_idx]),
22-
Applicability::MachineApplicable,
21+
|diag| {
22+
diag.span_suggestion(
23+
lit_span,
24+
"remove the underscore",
25+
format!("{}{suffix}", &lit_snip[..maybe_last_sep_idx]),
26+
Applicability::MachineApplicable,
27+
);
28+
},
2329
);
2430
} else {
25-
span_lint_and_sugg(
31+
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
32+
span_lint_and_then(
2633
cx,
2734
UNSEPARATED_LITERAL_SUFFIX,
2835
lit_span,
2936
format!("{sugg_type} type suffix should be separated by an underscore"),
30-
"add an underscore",
31-
format!("{}_{suffix}", &lit_snip[..=maybe_last_sep_idx]),
32-
Applicability::MachineApplicable,
37+
|diag| {
38+
diag.span_suggestion(
39+
lit_span,
40+
"add an underscore",
41+
format!("{}_{suffix}", &lit_snip[..=maybe_last_sep_idx]),
42+
Applicability::MachineApplicable,
43+
);
44+
},
3345
);
3446
}
3547
}

clippy_lints/src/module_style.rs

Lines changed: 18 additions & 15 deletions
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;
33
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
44
use rustc_lint::{EarlyContext, EarlyLintPass, Level, LintContext};
@@ -121,17 +121,19 @@ impl EarlyLintPass for ModStyle {
121121
for folder in &folder_segments {
122122
if !mod_folders.contains(folder) {
123123
if let Some((file, path)) = file_map.get(folder) {
124-
let mut correct = path.to_path_buf();
125-
correct.pop();
126-
correct.push(folder);
127-
correct.push("mod.rs");
128-
span_lint_and_help(
124+
span_lint_and_then(
129125
cx,
130126
SELF_NAMED_MODULE_FILES,
131127
Span::new(file.start_pos, file.start_pos, SyntaxContext::root(), None),
132128
format!("`mod.rs` files are required, found `{}`", path.display()),
133-
None,
134-
format!("move `{}` to `{}`", path.display(), correct.display(),),
129+
|diag| {
130+
let mut correct = path.to_path_buf();
131+
correct.pop();
132+
correct.push(folder);
133+
correct.push("mod.rs");
134+
135+
diag.help(format!("move `{}` to `{}`", path.display(), correct.display(),));
136+
},
135137
);
136138
}
137139
}
@@ -161,17 +163,18 @@ fn process_paths_for_mod_files<'a>(
161163
/// for code-sharing between tests.
162164
fn check_self_named_mod_exists(cx: &EarlyContext<'_>, path: &Path, file: &SourceFile) {
163165
if path.ends_with("mod.rs") && !path.starts_with("tests") {
164-
let mut mod_file = path.to_path_buf();
165-
mod_file.pop();
166-
mod_file.set_extension("rs");
167-
168-
span_lint_and_help(
166+
span_lint_and_then(
169167
cx,
170168
MOD_MODULE_FILES,
171169
Span::new(file.start_pos, file.start_pos, SyntaxContext::root(), None),
172170
format!("`mod.rs` files are not allowed, found `{}`", path.display()),
173-
None,
174-
format!("move `{}` to `{}`", path.display(), mod_file.display()),
171+
|diag| {
172+
let mut mod_file = path.to_path_buf();
173+
mod_file.pop();
174+
mod_file.set_extension("rs");
175+
176+
diag.help(format!("move `{}` to `{}`", path.display(), mod_file.display()));
177+
},
175178
);
176179
}
177180
}

clippy_lints/src/question_mark_used.rs

Lines changed: 6 additions & 4 deletions
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

33
use clippy_utils::macros::span_is_local;
44
use rustc_hir::{Expr, ExprKind, MatchSource};
@@ -39,13 +39,15 @@ impl<'tcx> LateLintPass<'tcx> for QuestionMarkUsed {
3939
return;
4040
}
4141

42-
span_lint_and_help(
42+
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
43+
span_lint_and_then(
4344
cx,
4445
QUESTION_MARK_USED,
4546
expr.span,
4647
"question mark operator was used",
47-
None,
48-
"consider using a custom macro or match expression",
48+
|diag| {
49+
diag.help("consider using a custom macro or match expression");
50+
},
4951
);
5052
}
5153
}

clippy_lints/src/ref_patterns.rs

Lines changed: 5 additions & 9 deletions
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::{BindingMode, Pat, PatKind};
33
use rustc_lint::{EarlyContext, EarlyLintPass};
44
use rustc_session::declare_lint_pass;
@@ -33,14 +33,10 @@ impl EarlyLintPass for RefPatterns {
3333
if let PatKind::Ident(BindingMode::REF, _, _) = pat.kind
3434
&& !pat.span.from_expansion()
3535
{
36-
span_lint_and_help(
37-
cx,
38-
REF_PATTERNS,
39-
pat.span,
40-
"usage of ref pattern",
41-
None,
42-
"consider using `&` for clarity instead",
43-
);
36+
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
37+
span_lint_and_then(cx, REF_PATTERNS, pat.span, "usage of ref pattern", |diag| {
38+
diag.help("consider using `&` for clarity instead");
39+
});
4440
}
4541
}
4642
}

clippy_lints/src/shadow.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clippy_utils::diagnostics::span_lint_and_note;
1+
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::source::snippet;
33
use clippy_utils::visitors::is_local_used;
44
use rustc_data_structures::fx::FxHashMap;
@@ -194,14 +194,9 @@ fn lint_shadow(cx: &LateContext<'_>, pat: &Pat<'_>, shadowed: HirId, span: Span)
194194
(SHADOW_UNRELATED, msg)
195195
},
196196
};
197-
span_lint_and_note(
198-
cx,
199-
lint,
200-
span,
201-
msg,
202-
Some(cx.tcx.hir().span(shadowed)),
203-
"previous binding is here",
204-
);
197+
span_lint_and_then(cx, lint, span, msg, |diag| {
198+
diag.span_note(cx.tcx.hir().span(shadowed), "previous binding is here");
199+
});
205200
}
206201

207202
/// Returns true if the expression is a simple transformation of a local binding such as `&x`

clippy_lints/src/single_char_lifetime_names.rs

Lines changed: 6 additions & 4 deletions
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::{GenericParam, GenericParamKind};
33
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
44
use rustc_middle::lint::in_external_macro;
@@ -48,13 +48,15 @@ impl EarlyLintPass for SingleCharLifetimeNames {
4848

4949
if let GenericParamKind::Lifetime = param.kind {
5050
if !param.is_placeholder && param.ident.as_str().len() <= 2 {
51-
span_lint_and_help(
51+
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
52+
span_lint_and_then(
5253
ctx,
5354
SINGLE_CHAR_LIFETIME_NAMES,
5455
param.ident.span,
5556
"single-character lifetime names are likely uninformative",
56-
None,
57-
"use a more informative name",
57+
|diag| {
58+
diag.help("use a more informative name");
59+
},
5860
);
5961
}
6062
}

clippy_lints/src/std_instead_of_core.rs

Lines changed: 11 additions & 5 deletions
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::is_from_proc_macro;
33
use rustc_errors::Applicability;
44
use rustc_hir::def::Res;
@@ -118,14 +118,20 @@ impl<'tcx> LateLintPass<'tcx> for StdReexports {
118118
_ => return,
119119
};
120120
if first_segment.ident.span != self.prev_span {
121-
span_lint_and_sugg(
121+
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
122+
span_lint_and_then(
122123
cx,
123124
lint,
124125
first_segment.ident.span,
125126
format!("used import from `{used_mod}` instead of `{replace_with}`"),
126-
format!("consider importing the item from `{replace_with}`"),
127-
replace_with.to_string(),
128-
Applicability::MachineApplicable,
127+
|diag| {
128+
diag.span_suggestion(
129+
first_segment.ident.span,
130+
format!("consider importing the item from `{replace_with}`"),
131+
replace_with.to_string(),
132+
Applicability::MachineApplicable,
133+
);
134+
},
129135
);
130136
self.prev_span = first_segment.ident.span;
131137
}

clippy_lints/src/strings.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clippy_utils::diagnostics::{span_lint, span_lint_and_help, span_lint_and_sugg};
1+
use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg, span_lint_and_then};
22
use clippy_utils::source::{snippet, snippet_with_applicability};
33
use clippy_utils::ty::is_type_lang_item;
44
use clippy_utils::{
@@ -399,17 +399,16 @@ impl<'tcx> LateLintPass<'tcx> for StrToString {
399399
&& let ty::Ref(_, ty, ..) = ty.kind()
400400
&& ty.is_str()
401401
{
402-
let mut applicability = Applicability::MachineApplicable;
403-
let snippet = snippet_with_applicability(cx, self_arg.span, "..", &mut applicability);
404-
405-
span_lint_and_sugg(
402+
span_lint_and_then(
406403
cx,
407404
STR_TO_STRING,
408405
expr.span,
409406
"`to_string()` called on a `&str`",
410-
"try",
411-
format!("{snippet}.to_owned()"),
412-
applicability,
407+
|diag| {
408+
let mut applicability = Applicability::MachineApplicable;
409+
let snippet = snippet_with_applicability(cx, self_arg.span, "..", &mut applicability);
410+
diag.span_suggestion(expr.span, "try", format!("{snippet}.to_owned()"), applicability);
411+
},
413412
);
414413
}
415414
}
@@ -455,13 +454,15 @@ impl<'tcx> LateLintPass<'tcx> for StringToString {
455454
&& let ty = cx.typeck_results().expr_ty(self_arg)
456455
&& is_type_lang_item(cx, ty, LangItem::String)
457456
{
458-
span_lint_and_help(
457+
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
458+
span_lint_and_then(
459459
cx,
460460
STRING_TO_STRING,
461461
expr.span,
462462
"`to_string()` called on a `String`",
463-
None,
464-
"consider using `.clone()`",
463+
|diag| {
464+
diag.help("consider using `.clone()`");
465+
},
465466
);
466467
}
467468
}

clippy_lints/src/suspicious_xor_used_as_pow.rs

Lines changed: 10 additions & 5 deletions
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::numeric_literal::NumericLiteral;
33
use clippy_utils::source::snippet;
44
use rustc_ast::LitKind;
@@ -43,14 +43,19 @@ impl LateLintPass<'_> for ConfusingXorAndPow {
4343
&& NumericLiteral::from_lit_kind(&snippet(cx, lit_right.span, ".."), &lit_right.node)
4444
.is_some_and(|x| x.is_decimal())
4545
{
46-
span_lint_and_sugg(
46+
span_lint_and_then(
4747
cx,
4848
SUSPICIOUS_XOR_USED_AS_POW,
4949
expr.span,
5050
"`^` is not the exponentiation operator",
51-
"did you mean to write",
52-
format!("{}.pow({})", lit_left.node, lit_right.node),
53-
Applicability::MaybeIncorrect,
51+
|diag| {
52+
diag.span_suggestion_verbose(
53+
expr.span,
54+
"did you mean to write",
55+
format!("{}.pow({})", lit_left.node, lit_right.node),
56+
Applicability::MaybeIncorrect,
57+
);
58+
},
5459
);
5560
}
5661
}

0 commit comments

Comments
 (0)