Skip to content

Commit 90c1963

Browse files
committed
Make restriction lint's use span_lint_and_then (n -> p)
1 parent d17f113 commit 90c1963

8 files changed

+106
-90
lines changed

clippy_lints/src/float_literal.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -106,16 +106,15 @@ impl<'tcx> LateLintPass<'tcx> for FloatLiteral {
106106
if is_whole && !sym_str.contains(['e', 'E']) {
107107
// Normalize the literal by stripping the fractional portion
108108
if sym_str.split('.').next().unwrap() != float_str {
109-
// If the type suffix is missing the suggestion would be
110-
// incorrectly interpreted as an integer so adding a `.0`
111-
// suffix to prevent that.
112-
113109
span_lint_and_then(
114110
cx,
115111
LOSSY_FLOAT_LITERAL,
116112
expr.span,
117113
"literal cannot be represented as the underlying type without loss of precision",
118114
|diag| {
115+
// If the type suffix is missing the suggestion would be
116+
// incorrectly interpreted as an integer so adding a `.0`
117+
// suffix to prevent that.
119118
if type_suffix.is_none() {
120119
float_str.push_str(".0");
121120
}

clippy_lints/src/methods/map_err_ignore.rs

-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ pub(super) fn check(cx: &LateContext<'_>, e: &Expr<'_>, arg: &Expr<'_>) {
3030
"`map_err(|_|...` wildcard pattern discards the original error",
3131
|diag| {
3232
diag.help(
33-
3433
"consider storing the original error as a source in the new error, or silence this warning using an ignored identifier (`.map_err(|_foo| ...`)",
3534
);
3635
},

clippy_lints/src/partial_pub_fields.rs

+9-17
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};
33
use rustc_lint::{EarlyContext, EarlyLintPass};
44
use rustc_session::declare_lint_pass;
@@ -57,24 +57,16 @@ impl EarlyLintPass for PartialPubFields {
5757

5858
for field in fields {
5959
if all_priv && field.vis.kind.is_pub() {
60-
span_lint_and_help(
61-
cx,
62-
PARTIAL_PUB_FIELDS,
63-
field.vis.span,
64-
msg,
65-
None,
66-
"consider using private field here",
67-
);
60+
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
61+
span_lint_and_then(cx, PARTIAL_PUB_FIELDS, field.vis.span, msg, |diag| {
62+
diag.help("consider using private field here");
63+
});
6864
return;
6965
} else if all_pub && !field.vis.kind.is_pub() {
70-
span_lint_and_help(
71-
cx,
72-
PARTIAL_PUB_FIELDS,
73-
field.vis.span,
74-
msg,
75-
None,
76-
"consider using public field here",
77-
);
66+
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
67+
span_lint_and_then(cx, PARTIAL_PUB_FIELDS, field.vis.span, msg, |diag| {
68+
diag.help("consider using public field here");
69+
});
7870
return;
7971
}
8072
}

clippy_lints/src/pattern_type_mismatch.rs

+16-14
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::{
33
intravisit, Body, Expr, ExprKind, FnDecl, LetExpr, LocalSource, Mutability, Pat, PatKind, Stmt, StmtKind,
44
};
@@ -133,23 +133,25 @@ enum DerefPossible {
133133
fn apply_lint(cx: &LateContext<'_>, pat: &Pat<'_>, deref_possible: DerefPossible) -> bool {
134134
let maybe_mismatch = find_first_mismatch(cx, pat);
135135
if let Some((span, mutability, level)) = maybe_mismatch {
136-
span_lint_and_help(
136+
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
137+
span_lint_and_then(
137138
cx,
138139
PATTERN_TYPE_MISMATCH,
139140
span,
140141
"type of pattern does not match the expression type",
141-
None,
142-
format!(
143-
"{}explicitly match against a `{}` pattern and adjust the enclosed variable bindings",
144-
match (deref_possible, level) {
145-
(DerefPossible::Possible, Level::Top) => "use `*` to dereference the match expression or ",
146-
_ => "",
147-
},
148-
match mutability {
149-
Mutability::Mut => "&mut _",
150-
Mutability::Not => "&_",
151-
},
152-
),
142+
|diag| {
143+
diag.help(format!(
144+
"{}explicitly match against a `{}` pattern and adjust the enclosed variable bindings",
145+
match (deref_possible, level) {
146+
(DerefPossible::Possible, Level::Top) => "use `*` to dereference the match expression or ",
147+
_ => "",
148+
},
149+
match mutability {
150+
Mutability::Mut => "&mut _",
151+
Mutability::Not => "&_",
152+
},
153+
));
154+
},
153155
);
154156
true
155157
} else {

clippy_lints/src/pub_use.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::{Item, ItemKind, VisibilityKind};
33
use rustc_lint::{EarlyContext, EarlyLintPass};
44
use rustc_session::declare_lint_pass;
@@ -42,14 +42,10 @@ impl EarlyLintPass for PubUse {
4242
if let ItemKind::Use(_) = item.kind
4343
&& let VisibilityKind::Public = item.vis.kind
4444
{
45-
span_lint_and_help(
46-
cx,
47-
PUB_USE,
48-
item.span,
49-
"using `pub use`",
50-
None,
51-
"move the exported item to a public module instead",
52-
);
45+
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
46+
span_lint_and_then(cx, PUB_USE, item.span, "using `pub use`", |diag| {
47+
diag.help("move the exported item to a public module instead");
48+
});
5349
}
5450
}
5551
}

clippy_lints/src/unicode.rs

+35-29
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_lint_allowed;
33
use clippy_utils::macros::span_is_local;
44
use clippy_utils::source::snippet;
@@ -105,45 +105,51 @@ fn check_str(cx: &LateContext<'_>, span: Span, id: HirId) {
105105

106106
let string = snippet(cx, span, "");
107107
if string.chars().any(|c| ['\u{200B}', '\u{ad}', '\u{2060}'].contains(&c)) {
108-
span_lint_and_sugg(
109-
cx,
110-
INVISIBLE_CHARACTERS,
111-
span,
112-
"invisible character detected",
113-
"consider replacing the string with",
114-
string
115-
.replace('\u{200B}', "\\u{200B}")
116-
.replace('\u{ad}', "\\u{AD}")
117-
.replace('\u{2060}', "\\u{2060}"),
118-
Applicability::MachineApplicable,
119-
);
108+
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
109+
span_lint_and_then(cx, INVISIBLE_CHARACTERS, span, "invisible character detected", |diag| {
110+
diag.span_suggestion(
111+
span,
112+
"consider replacing the string with",
113+
string
114+
.replace('\u{200B}', "\\u{200B}")
115+
.replace('\u{ad}', "\\u{AD}")
116+
.replace('\u{2060}', "\\u{2060}"),
117+
Applicability::MachineApplicable,
118+
);
119+
});
120120
}
121121

122122
if string.chars().any(|c| c as u32 > 0x7F) {
123-
span_lint_and_sugg(
123+
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
124+
span_lint_and_then(
124125
cx,
125126
NON_ASCII_LITERAL,
126127
span,
127128
"literal non-ASCII character detected",
128-
"consider replacing the string with",
129-
if is_lint_allowed(cx, UNICODE_NOT_NFC, id) {
130-
escape(string.chars())
131-
} else {
132-
escape(string.nfc())
129+
|diag| {
130+
diag.span_suggestion(
131+
span,
132+
"consider replacing the string with",
133+
if is_lint_allowed(cx, UNICODE_NOT_NFC, id) {
134+
escape(string.chars())
135+
} else {
136+
escape(string.nfc())
137+
},
138+
Applicability::MachineApplicable,
139+
);
133140
},
134-
Applicability::MachineApplicable,
135141
);
136142
}
137143

138144
if is_lint_allowed(cx, NON_ASCII_LITERAL, id) && string.chars().zip(string.nfc()).any(|(a, b)| a != b) {
139-
span_lint_and_sugg(
140-
cx,
141-
UNICODE_NOT_NFC,
142-
span,
143-
"non-NFC Unicode sequence detected",
144-
"consider replacing the string with",
145-
string.nfc().collect::<String>(),
146-
Applicability::MachineApplicable,
147-
);
145+
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
146+
span_lint_and_then(cx, UNICODE_NOT_NFC, span, "non-NFC Unicode sequence detected", |diag| {
147+
diag.span_suggestion(
148+
span,
149+
"consider replacing the string with",
150+
string.nfc().collect::<String>(),
151+
Applicability::MachineApplicable,
152+
);
153+
});
148154
}
149155
}

clippy_lints/src/visibility.rs

+30-13
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_opt;
33
use rustc_ast::ast::{Item, VisibilityKind};
44
use rustc_errors::Applicability;
@@ -85,14 +85,19 @@ impl EarlyLintPass for Visibility {
8585
if **path == kw::SelfLower
8686
&& let Some(false) = is_from_proc_macro(cx, item.vis.span)
8787
{
88-
span_lint_and_sugg(
88+
span_lint_and_then(
8989
cx,
9090
NEEDLESS_PUB_SELF,
9191
item.vis.span,
9292
format!("unnecessary `pub({}self)`", if *shorthand { "" } else { "in " }),
93-
"remove it",
94-
String::new(),
95-
Applicability::MachineApplicable,
93+
|diag| {
94+
diag.span_suggestion_hidden(
95+
item.vis.span,
96+
"remove it",
97+
String::new(),
98+
Applicability::MachineApplicable,
99+
);
100+
},
96101
);
97102
}
98103

@@ -101,29 +106,41 @@ impl EarlyLintPass for Visibility {
101106
&& let [.., last] = &*path.segments
102107
&& let Some(false) = is_from_proc_macro(cx, item.vis.span)
103108
{
104-
span_lint_and_sugg(
109+
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
110+
span_lint_and_then(
105111
cx,
106112
PUB_WITHOUT_SHORTHAND,
107113
item.vis.span,
108114
"usage of `pub` with `in`",
109-
"remove it",
110-
format!("pub({})", last.ident),
111-
Applicability::MachineApplicable,
115+
|diag| {
116+
diag.span_suggestion(
117+
item.vis.span,
118+
"remove it",
119+
format!("pub({})", last.ident),
120+
Applicability::MachineApplicable,
121+
);
122+
},
112123
);
113124
}
114125

115126
if *shorthand
116127
&& let [.., last] = &*path.segments
117128
&& let Some(false) = is_from_proc_macro(cx, item.vis.span)
118129
{
119-
span_lint_and_sugg(
130+
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
131+
span_lint_and_then(
120132
cx,
121133
PUB_WITH_SHORTHAND,
122134
item.vis.span,
123135
"usage of `pub` without `in`",
124-
"add it",
125-
format!("pub(in {})", last.ident),
126-
Applicability::MachineApplicable,
136+
|diag| {
137+
diag.span_suggestion(
138+
item.vis.span,
139+
"add it",
140+
format!("pub(in {})", last.ident),
141+
Applicability::MachineApplicable,
142+
);
143+
},
127144
);
128145
}
129146
}

tests/ui/needless_pub_self.stderr

+8-3
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,27 @@ error: unnecessary `pub(self)`
22
--> tests/ui/needless_pub_self.rs:13:1
33
|
44
LL | pub(self) fn a() {}
5-
| ^^^^^^^^^ help: remove it
5+
| ^^^^^^^^^
66
|
77
= note: `-D clippy::needless-pub-self` implied by `-D warnings`
88
= help: to override `-D warnings` add `#[allow(clippy::needless_pub_self)]`
9+
= help: remove it
910

1011
error: unnecessary `pub(in self)`
1112
--> tests/ui/needless_pub_self.rs:14:1
1213
|
1314
LL | pub(in self) fn b() {}
14-
| ^^^^^^^^^^^^ help: remove it
15+
| ^^^^^^^^^^^^
16+
|
17+
= help: remove it
1518

1619
error: unnecessary `pub(self)`
1720
--> tests/ui/needless_pub_self.rs:20:5
1821
|
1922
LL | pub(self) fn f() {}
20-
| ^^^^^^^^^ help: remove it
23+
| ^^^^^^^^^
24+
|
25+
= help: remove it
2126

2227
error: aborting due to 3 previous errors
2328

0 commit comments

Comments
 (0)