Skip to content

Commit bc8fc6b

Browse files
committed
Make restriction lint's use span_lint_and_then (i -> l)
1 parent 7f0ed11 commit bc8fc6b

7 files changed

+245
-92
lines changed

clippy_lints/src/asm_syntax.rs

Lines changed: 9 additions & 13 deletions
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/float_literal.rs

Lines changed: 26 additions & 14 deletions
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};
@@ -109,29 +109,41 @@ impl<'tcx> LateLintPass<'tcx> for FloatLiteral {
109109
// If the type suffix is missing the suggestion would be
110110
// incorrectly interpreted as an integer so adding a `.0`
111111
// suffix to prevent that.
112-
if type_suffix.is_none() {
113-
float_str.push_str(".0");
114-
}
115-
116-
span_lint_and_sugg(
112+
113+
span_lint_and_then(
117114
cx,
118115
LOSSY_FLOAT_LITERAL,
119116
expr.span,
120117
"literal cannot be represented as the underlying type without loss of precision",
121-
"consider changing the type or replacing it with",
122-
numeric_literal::format(&float_str, type_suffix, true),
123-
Applicability::MachineApplicable,
118+
|diag| {
119+
if type_suffix.is_none() {
120+
float_str.push_str(".0");
121+
}
122+
diag.span_suggestion_with_style(
123+
expr.span,
124+
"consider changing the type or replacing it with",
125+
numeric_literal::format(&float_str, type_suffix, true),
126+
Applicability::MachineApplicable,
127+
SuggestionStyle::ShowAlways,
128+
);
129+
},
124130
);
125131
}
126132
} else if digits > max as usize && float_str.len() < sym_str.len() {
127-
span_lint_and_sugg(
133+
span_lint_and_then(
128134
cx,
129135
EXCESSIVE_PRECISION,
130136
expr.span,
131137
"float has excessive precision",
132-
"consider changing the type or truncating it to",
133-
numeric_literal::format(&float_str, type_suffix, true),
134-
Applicability::MachineApplicable,
138+
|diag| {
139+
diag.span_suggestion_with_style(
140+
expr.span,
141+
"consider changing the type or truncating it to",
142+
numeric_literal::format(&float_str, type_suffix, true),
143+
Applicability::MachineApplicable,
144+
SuggestionStyle::ShowAlways,
145+
);
146+
},
135147
);
136148
}
137149
}

clippy_lints/src/large_include_file.rs

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

Lines changed: 36 additions & 22 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 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
}
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_hir as hir;
33
use rustc_lint::LateContext;
44

@@ -15,13 +15,9 @@ pub(crate) fn check<'tcx>(
1515
&& cx.typeck_results().expr_ty(left).is_integral()
1616
&& cx.typeck_results().expr_ty(right).is_integral()
1717
{
18-
span_lint_and_help(
19-
cx,
20-
INTEGER_DIVISION,
21-
expr.span,
22-
"integer division",
23-
None,
24-
"division of integers may cause loss of precision. consider using floats",
25-
);
18+
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
19+
span_lint_and_then(cx, INTEGER_DIVISION, expr.span, "integer division", |diag| {
20+
diag.help("division of integers may cause loss of precision. consider using floats");
21+
});
2622
}
2723
}

0 commit comments

Comments
 (0)