Skip to content

Commit 345c94c

Browse files
committed
Auto merge of #13144 - xFrednet:07797-restriction-and-then-what, r=y21
Make restriction lint's use `span_lint_and_then` (i -> p) This migrates a few restriction lints to use `span_lint_and_then`. This change is motivated by #7797. I've also cleaned up some lint message. Mostly minor stuff. For example: suggestions with a longer message than `"try"` now use `SuggestionStyle::ShowAlways` --- cc: #7797 brother PR of: #13136 changelog: none
2 parents 533c377 + 90c1963 commit 345c94c

20 files changed

+409
-226
lines changed

clippy_lints/src/asm_syntax.rs

+9-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::fmt;
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, InlineAsmOptions};
55
use rustc_ast::{InlineAsm, Item, ItemKind};
66
use rustc_lint::{EarlyContext, EarlyLintPass, Lint, LintContext};
@@ -49,14 +49,10 @@ fn check_asm_syntax(
4949
};
5050

5151
if style == check_for {
52-
span_lint_and_help(
53-
cx,
54-
lint,
55-
span,
56-
format!("{style} x86 assembly syntax used"),
57-
None,
58-
format!("use {} x86 assembly syntax", !style),
59-
);
52+
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
53+
span_lint_and_then(cx, lint, span, format!("{style} x86 assembly syntax used"), |diag| {
54+
diag.help(format!("use {} x86 assembly syntax", !style));
55+
});
6056
}
6157
}
6258
}
@@ -98,13 +94,13 @@ declare_lint_pass!(InlineAsmX86IntelSyntax => [INLINE_ASM_X86_INTEL_SYNTAX]);
9894
impl EarlyLintPass for InlineAsmX86IntelSyntax {
9995
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
10096
if let ExprKind::InlineAsm(inline_asm) = &expr.kind {
101-
check_asm_syntax(Self::get_lints()[0], cx, inline_asm, expr.span, AsmStyle::Intel);
97+
check_asm_syntax(INLINE_ASM_X86_INTEL_SYNTAX, cx, inline_asm, expr.span, AsmStyle::Intel);
10298
}
10399
}
104100

105101
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
106102
if let ItemKind::GlobalAsm(inline_asm) = &item.kind {
107-
check_asm_syntax(Self::get_lints()[0], cx, inline_asm, item.span, AsmStyle::Intel);
103+
check_asm_syntax(INLINE_ASM_X86_INTEL_SYNTAX, cx, inline_asm, item.span, AsmStyle::Intel);
108104
}
109105
}
110106
}
@@ -146,13 +142,13 @@ declare_lint_pass!(InlineAsmX86AttSyntax => [INLINE_ASM_X86_ATT_SYNTAX]);
146142
impl EarlyLintPass for InlineAsmX86AttSyntax {
147143
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
148144
if let ExprKind::InlineAsm(inline_asm) = &expr.kind {
149-
check_asm_syntax(Self::get_lints()[0], cx, inline_asm, expr.span, AsmStyle::Att);
145+
check_asm_syntax(INLINE_ASM_X86_ATT_SYNTAX, cx, inline_asm, expr.span, AsmStyle::Att);
150146
}
151147
}
152148

153149
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
154150
if let ItemKind::GlobalAsm(inline_asm) = &item.kind {
155-
check_asm_syntax(Self::get_lints()[0], cx, inline_asm, item.span, AsmStyle::Att);
151+
check_asm_syntax(INLINE_ASM_X86_ATT_SYNTAX, cx, inline_asm, item.span, AsmStyle::Att);
156152
}
157153
}
158154
}

clippy_lints/src/drop_forget_ref.rs

+9-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::is_must_use_func_call;
33
use clippy_utils::ty::{is_copy, is_must_use_ty, is_type_lang_item};
44
use rustc_hir::{Arm, Expr, ExprKind, LangItem, Node};
@@ -126,14 +126,14 @@ impl<'tcx> LateLintPass<'tcx> for DropForgetRef {
126126
},
127127
_ => return,
128128
};
129-
span_lint_and_note(
130-
cx,
131-
lint,
132-
expr.span,
133-
msg,
134-
note_span,
135-
format!("argument has type `{arg_ty}`"),
136-
);
129+
span_lint_and_then(cx, lint, expr.span, msg, |diag| {
130+
let note = format!("argument has type `{arg_ty}`");
131+
if let Some(span) = note_span {
132+
diag.span_note(span, note);
133+
} else {
134+
diag.note(note);
135+
}
136+
});
137137
}
138138
}
139139
}

clippy_lints/src/float_literal.rs

+28-17
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use clippy_utils::diagnostics::span_lint_and_sugg;
1+
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::numeric_literal;
33
use rustc_ast::ast::{self, LitFloatType, LitKind};
4-
use rustc_errors::Applicability;
4+
use rustc_errors::{Applicability, SuggestionStyle};
55
use rustc_hir as hir;
66
use rustc_lint::{LateContext, LateLintPass};
77
use rustc_middle::ty::{self, FloatTy};
@@ -105,32 +105,43 @@ impl<'tcx> LateLintPass<'tcx> for FloatLiteral {
105105
if is_whole && !sym_str.contains(['e', 'E']) {
106106
// Normalize the literal by stripping the fractional portion
107107
if sym_str.split('.').next().unwrap() != float_str {
108-
// If the type suffix is missing the suggestion would be
109-
// incorrectly interpreted as an integer so adding a `.0`
110-
// suffix to prevent that.
111-
if type_suffix.is_none() {
112-
float_str.push_str(".0");
113-
}
114-
115-
span_lint_and_sugg(
108+
span_lint_and_then(
116109
cx,
117110
LOSSY_FLOAT_LITERAL,
118111
expr.span,
119112
"literal cannot be represented as the underlying type without loss of precision",
120-
"consider changing the type or replacing it with",
121-
numeric_literal::format(&float_str, type_suffix, true),
122-
Applicability::MachineApplicable,
113+
|diag| {
114+
// If the type suffix is missing the suggestion would be
115+
// incorrectly interpreted as an integer so adding a `.0`
116+
// suffix to prevent that.
117+
if type_suffix.is_none() {
118+
float_str.push_str(".0");
119+
}
120+
diag.span_suggestion_with_style(
121+
expr.span,
122+
"consider changing the type or replacing it with",
123+
numeric_literal::format(&float_str, type_suffix, true),
124+
Applicability::MachineApplicable,
125+
SuggestionStyle::ShowAlways,
126+
);
127+
},
123128
);
124129
}
125130
} else if digits > max as usize && float_str.len() < sym_str.len() {
126-
span_lint_and_sugg(
131+
span_lint_and_then(
127132
cx,
128133
EXCESSIVE_PRECISION,
129134
expr.span,
130135
"float has excessive precision",
131-
"consider changing the type or truncating it to",
132-
numeric_literal::format(&float_str, type_suffix, true),
133-
Applicability::MachineApplicable,
136+
|diag| {
137+
diag.span_suggestion_with_style(
138+
expr.span,
139+
"consider changing the type or truncating it to",
140+
numeric_literal::format(&float_str, type_suffix, true),
141+
Applicability::MachineApplicable,
142+
SuggestionStyle::ShowAlways,
143+
);
144+
},
134145
);
135146
}
136147
}

clippy_lints/src/inherent_impl.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! lint on inherent implementations
22
3-
use clippy_utils::diagnostics::span_lint_and_note;
3+
use clippy_utils::diagnostics::span_lint_and_then;
44
use clippy_utils::is_lint_allowed;
55
use rustc_data_structures::fx::FxHashMap;
66
use rustc_hir::def_id::LocalDefId;
@@ -105,13 +105,14 @@ impl<'tcx> LateLintPass<'tcx> for MultipleInherentImpl {
105105
// `TyCtxt::crate_inherent_impls` doesn't have a defined order. Sort the lint output first.
106106
lint_spans.sort_by_key(|x| x.0.lo());
107107
for (span, first_span) in lint_spans {
108-
span_lint_and_note(
108+
span_lint_and_then(
109109
cx,
110110
MULTIPLE_INHERENT_IMPL,
111111
span,
112112
"multiple implementations of this structure",
113-
Some(first_span),
114-
"first implementation here",
113+
|diag| {
114+
diag.span_note(first_span, "first implementation here");
115+
},
115116
);
116117
}
117118
}

clippy_lints/src/large_include_file.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clippy_config::Conf;
2-
use clippy_utils::diagnostics::span_lint_and_note;
2+
use clippy_utils::diagnostics::span_lint_and_then;
33
use clippy_utils::macros::root_macro_call_first_node;
44
use rustc_ast::LitKind;
55
use rustc_hir::{Expr, ExprKind};
@@ -66,16 +66,18 @@ impl LateLintPass<'_> for LargeIncludeFile {
6666
&& (cx.tcx.is_diagnostic_item(sym::include_bytes_macro, macro_call.def_id)
6767
|| cx.tcx.is_diagnostic_item(sym::include_str_macro, macro_call.def_id))
6868
{
69-
span_lint_and_note(
69+
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
70+
span_lint_and_then(
7071
cx,
7172
LARGE_INCLUDE_FILE,
7273
expr.span.source_callsite(),
7374
"attempted to include a large file",
74-
None,
75-
format!(
76-
"the configuration allows a maximum size of {} bytes",
77-
self.max_file_size
78-
),
75+
|diag| {
76+
diag.note(format!(
77+
"the configuration allows a maximum size of {} bytes",
78+
self.max_file_size
79+
));
80+
},
7981
);
8082
}
8183
}

clippy_lints/src/let_underscore.rs

+36-22
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::{implements_trait, is_must_use_ty, match_type};
33
use clippy_utils::{is_from_proc_macro, is_must_use_func_call, paths};
44
use rustc_hir::{LetStmt, LocalSource, PatKind};
@@ -149,43 +149,53 @@ impl<'tcx> LateLintPass<'tcx> for LetUnderscore {
149149
GenericArgKind::Lifetime(_) | GenericArgKind::Const(_) => false,
150150
});
151151
if contains_sync_guard {
152-
span_lint_and_help(
152+
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
153+
span_lint_and_then(
153154
cx,
154155
LET_UNDERSCORE_LOCK,
155156
local.span,
156157
"non-binding `let` on a synchronization lock",
157-
None,
158-
"consider using an underscore-prefixed named \
159-
binding or dropping explicitly with `std::mem::drop`",
158+
|diag| {
159+
diag.help(
160+
"consider using an underscore-prefixed named \
161+
binding or dropping explicitly with `std::mem::drop`",
162+
);
163+
},
160164
);
161165
} else if let Some(future_trait_def_id) = cx.tcx.lang_items().future_trait()
162166
&& implements_trait(cx, cx.typeck_results().expr_ty(init), future_trait_def_id, &[])
163167
{
164-
span_lint_and_help(
168+
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
169+
span_lint_and_then(
165170
cx,
166171
LET_UNDERSCORE_FUTURE,
167172
local.span,
168173
"non-binding `let` on a future",
169-
None,
170-
"consider awaiting the future or dropping explicitly with `std::mem::drop`",
174+
|diag| {
175+
diag.help("consider awaiting the future or dropping explicitly with `std::mem::drop`");
176+
},
171177
);
172178
} else if is_must_use_ty(cx, cx.typeck_results().expr_ty(init)) {
173-
span_lint_and_help(
179+
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
180+
span_lint_and_then(
174181
cx,
175182
LET_UNDERSCORE_MUST_USE,
176183
local.span,
177184
"non-binding `let` on an expression with `#[must_use]` type",
178-
None,
179-
"consider explicitly using expression value",
185+
|diag| {
186+
diag.help("consider explicitly using expression value");
187+
},
180188
);
181189
} else if is_must_use_func_call(cx, init) {
182-
span_lint_and_help(
190+
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
191+
span_lint_and_then(
183192
cx,
184193
LET_UNDERSCORE_MUST_USE,
185194
local.span,
186195
"non-binding `let` on a result of a `#[must_use]` function",
187-
None,
188-
"consider explicitly using function result",
196+
|diag| {
197+
diag.help("consider explicitly using function result");
198+
},
189199
);
190200
}
191201

@@ -204,18 +214,22 @@ impl<'tcx> LateLintPass<'tcx> for LetUnderscore {
204214
return;
205215
}
206216

207-
span_lint_and_help(
217+
span_lint_and_then(
208218
cx,
209219
LET_UNDERSCORE_UNTYPED,
210220
local.span,
211221
"non-binding `let` without a type annotation",
212-
Some(Span::new(
213-
local.pat.span.hi(),
214-
local.pat.span.hi() + BytePos(1),
215-
local.pat.span.ctxt(),
216-
local.pat.span.parent(),
217-
)),
218-
"consider adding a type annotation",
222+
|diag| {
223+
diag.span_help(
224+
Span::new(
225+
local.pat.span.hi(),
226+
local.pat.span.hi() + BytePos(1),
227+
local.pat.span.ctxt(),
228+
local.pat.span.parent(),
229+
),
230+
"consider adding a type annotation",
231+
);
232+
},
219233
);
220234
}
221235
}

clippy_lints/src/methods/map_err_ignore.rs

+8-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_diagnostic_item;
33
use rustc_hir::{CaptureBy, Closure, Expr, ExprKind, PatKind};
44
use rustc_lint::LateContext;
@@ -22,13 +22,17 @@ pub(super) fn check(cx: &LateContext<'_>, e: &Expr<'_>, arg: &Expr<'_>) {
2222
{
2323
// span the area of the closure capture and warn that the
2424
// original error will be thrown away
25-
span_lint_and_help(
25+
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
26+
span_lint_and_then(
2627
cx,
2728
MAP_ERR_IGNORE,
2829
fn_decl_span,
2930
"`map_err(|_|...` wildcard pattern discards the original error",
30-
None,
31-
"consider storing the original error as a source in the new error, or silence this warning using an ignored identifier (`.map_err(|_foo| ...`)",
31+
|diag| {
32+
diag.help(
33+
"consider storing the original error as a source in the new error, or silence this warning using an ignored identifier (`.map_err(|_foo| ...`)",
34+
);
35+
},
3236
);
3337
}
3438
}

clippy_lints/src/missing_assert_message.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::is_in_test;
33
use clippy_utils::macros::{find_assert_args, find_assert_eq_args, root_macro_call_first_node, PanicExpn};
44
use rustc_hir::Expr;
@@ -79,13 +79,15 @@ impl<'tcx> LateLintPass<'tcx> for MissingAssertMessage {
7979
};
8080

8181
if let PanicExpn::Empty = panic_expn {
82-
span_lint_and_help(
82+
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
83+
span_lint_and_then(
8384
cx,
8485
MISSING_ASSERT_MESSAGE,
8586
macro_call.span,
8687
"assert without any message",
87-
None,
88-
"consider describing why the failing assert is problematic",
88+
|diag| {
89+
diag.help("consider describing why the failing assert is problematic");
90+
},
8991
);
9092
}
9193
}

clippy_lints/src/missing_trait_methods.rs

+6-6
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::is_lint_allowed;
33
use clippy_utils::macros::span_is_local;
44
use rustc_hir::def_id::DefIdMap;
@@ -83,15 +83,15 @@ impl<'tcx> LateLintPass<'tcx> for MissingTraitMethods {
8383
cx.tcx.with_stable_hashing_context(|hcx| {
8484
for assoc in provided.values_sorted(&hcx, true) {
8585
let source_map = cx.tcx.sess.source_map();
86-
let definition_span = source_map.guess_head_span(cx.tcx.def_span(assoc.def_id));
87-
88-
span_lint_and_help(
86+
span_lint_and_then(
8987
cx,
9088
MISSING_TRAIT_METHODS,
9189
source_map.guess_head_span(item.span),
9290
format!("missing trait method provided by default: `{}`", assoc.name),
93-
Some(definition_span),
94-
"implement the method",
91+
|diag| {
92+
let definition_span = source_map.guess_head_span(cx.tcx.def_span(assoc.def_id));
93+
diag.span_help(definition_span, "implement the method");
94+
},
9595
);
9696
}
9797
});

0 commit comments

Comments
 (0)