Skip to content

Commit 7de9c20

Browse files
committed
Make restriction lint's use span_lint_and_then (q -> s)
1 parent 1ec5025 commit 7de9c20

12 files changed

+163
-101
lines changed

clippy_lints/src/matches/rest_pat_in_fully_bound_struct.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_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

+21-9
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/question_mark_used.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

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

+5-9
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

+4-9
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

+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::{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

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_config::msrvs::Msrv;
22
use clippy_config::Conf;
3-
use clippy_utils::diagnostics::span_lint_and_sugg;
3+
use clippy_utils::diagnostics::span_lint_and_then;
44
use clippy_utils::is_from_proc_macro;
55
use rustc_attr::{StabilityLevel, StableSince};
66
use rustc_errors::Applicability;
@@ -136,14 +136,20 @@ impl<'tcx> LateLintPass<'tcx> for StdReexports {
136136
_ => return,
137137
};
138138
if first_segment.ident.span != self.prev_span {
139-
span_lint_and_sugg(
139+
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
140+
span_lint_and_then(
140141
cx,
141142
lint,
142143
first_segment.ident.span,
143144
format!("used import from `{used_mod}` instead of `{replace_with}`"),
144-
format!("consider importing the item from `{replace_with}`"),
145-
replace_with.to_string(),
146-
Applicability::MachineApplicable,
145+
|diag| {
146+
diag.span_suggestion(
147+
first_segment.ident.span,
148+
format!("consider importing the item from `{replace_with}`"),
149+
replace_with.to_string(),
150+
Applicability::MachineApplicable,
151+
);
152+
},
147153
);
148154
self.prev_span = first_segment.ident.span;
149155
}

clippy_lints/src/strings.rs

+12-11
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

+10-5
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
}

clippy_lints/src/types/rc_buffer.rs

+37-25
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::source::snippet_with_applicability;
33
use clippy_utils::{path_def_id, qpath_generic_tys};
44
use rustc_errors::Applicability;
@@ -13,14 +13,15 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_
1313
let app = Applicability::Unspecified;
1414
if cx.tcx.is_diagnostic_item(sym::Rc, def_id) {
1515
if let Some(alternate) = match_buffer_type(cx, qpath) {
16-
span_lint_and_sugg(
16+
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
17+
span_lint_and_then(
1718
cx,
1819
RC_BUFFER,
1920
hir_ty.span,
2021
"usage of `Rc<T>` when T is a buffer type",
21-
"try",
22-
format!("Rc<{alternate}>"),
23-
app,
22+
|diag| {
23+
diag.span_suggestion(hir_ty.span, "try", format!("Rc<{alternate}>"), app);
24+
},
2425
);
2526
} else {
2627
let Some(ty) = qpath_generic_tys(qpath).next() else {
@@ -35,31 +36,37 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_
3536
Some(ty) => ty.span,
3637
None => return false,
3738
};
38-
let mut applicability = app;
39-
span_lint_and_sugg(
39+
span_lint_and_then(
4040
cx,
4141
RC_BUFFER,
4242
hir_ty.span,
4343
"usage of `Rc<T>` when T is a buffer type",
44-
"try",
45-
format!(
46-
"Rc<[{}]>",
47-
snippet_with_applicability(cx, inner_span, "..", &mut applicability)
48-
),
49-
app,
44+
|diag| {
45+
let mut applicability = app;
46+
diag.span_suggestion(
47+
hir_ty.span,
48+
"try",
49+
format!(
50+
"Rc<[{}]>",
51+
snippet_with_applicability(cx, inner_span, "..", &mut applicability)
52+
),
53+
app,
54+
);
55+
},
5056
);
5157
return true;
5258
}
5359
} else if cx.tcx.is_diagnostic_item(sym::Arc, def_id) {
5460
if let Some(alternate) = match_buffer_type(cx, qpath) {
55-
span_lint_and_sugg(
61+
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
62+
span_lint_and_then(
5663
cx,
5764
RC_BUFFER,
5865
hir_ty.span,
5966
"usage of `Arc<T>` when T is a buffer type",
60-
"try",
61-
format!("Arc<{alternate}>"),
62-
app,
67+
|diag| {
68+
diag.span_suggestion(hir_ty.span, "try", format!("Arc<{alternate}>"), app);
69+
},
6370
);
6471
} else if let Some(ty) = qpath_generic_tys(qpath).next() {
6572
let Some(id) = path_def_id(cx, ty) else { return false };
@@ -71,18 +78,23 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_
7178
Some(ty) => ty.span,
7279
None => return false,
7380
};
74-
let mut applicability = app;
75-
span_lint_and_sugg(
81+
span_lint_and_then(
7682
cx,
7783
RC_BUFFER,
7884
hir_ty.span,
7985
"usage of `Arc<T>` when T is a buffer type",
80-
"try",
81-
format!(
82-
"Arc<[{}]>",
83-
snippet_with_applicability(cx, inner_span, "..", &mut applicability)
84-
),
85-
app,
86+
|diag| {
87+
let mut applicability = app;
88+
diag.span_suggestion(
89+
hir_ty.span,
90+
"try",
91+
format!(
92+
"Arc<[{}]>",
93+
snippet_with_applicability(cx, inner_span, "..", &mut applicability)
94+
),
95+
app,
96+
);
97+
},
8698
);
8799
return true;
88100
}

0 commit comments

Comments
 (0)