Make restriction lint's use span_lint_and_then (n -> p)

This commit is contained in:
xFrednet 2024-07-21 20:17:38 +02:00
parent d17f113474
commit 90c19635fd
No known key found for this signature in database
GPG key ID: F5C59D0E669E5302
8 changed files with 106 additions and 90 deletions

View file

@ -106,16 +106,15 @@ impl<'tcx> LateLintPass<'tcx> for FloatLiteral {
if is_whole && !sym_str.contains(['e', 'E']) {
// Normalize the literal by stripping the fractional portion
if sym_str.split('.').next().unwrap() != float_str {
// If the type suffix is missing the suggestion would be
// incorrectly interpreted as an integer so adding a `.0`
// suffix to prevent that.
span_lint_and_then(
cx,
LOSSY_FLOAT_LITERAL,
expr.span,
"literal cannot be represented as the underlying type without loss of precision",
|diag| {
// If the type suffix is missing the suggestion would be
// incorrectly interpreted as an integer so adding a `.0`
// suffix to prevent that.
if type_suffix.is_none() {
float_str.push_str(".0");
}

View file

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

View file

@ -1,4 +1,4 @@
use clippy_utils::diagnostics::span_lint_and_help;
use clippy_utils::diagnostics::span_lint_and_then;
use rustc_ast::ast::{Item, ItemKind};
use rustc_lint::{EarlyContext, EarlyLintPass};
use rustc_session::declare_lint_pass;
@ -57,24 +57,16 @@ impl EarlyLintPass for PartialPubFields {
for field in fields {
if all_priv && field.vis.kind.is_pub() {
span_lint_and_help(
cx,
PARTIAL_PUB_FIELDS,
field.vis.span,
msg,
None,
"consider using private field here",
);
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
span_lint_and_then(cx, PARTIAL_PUB_FIELDS, field.vis.span, msg, |diag| {
diag.help("consider using private field here");
});
return;
} else if all_pub && !field.vis.kind.is_pub() {
span_lint_and_help(
cx,
PARTIAL_PUB_FIELDS,
field.vis.span,
msg,
None,
"consider using public field here",
);
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
span_lint_and_then(cx, PARTIAL_PUB_FIELDS, field.vis.span, msg, |diag| {
diag.help("consider using public field here");
});
return;
}
}

View file

@ -1,4 +1,4 @@
use clippy_utils::diagnostics::span_lint_and_help;
use clippy_utils::diagnostics::span_lint_and_then;
use rustc_hir::{
intravisit, Body, Expr, ExprKind, FnDecl, LetExpr, LocalSource, Mutability, Pat, PatKind, Stmt, StmtKind,
};
@ -133,23 +133,25 @@ enum DerefPossible {
fn apply_lint(cx: &LateContext<'_>, pat: &Pat<'_>, deref_possible: DerefPossible) -> bool {
let maybe_mismatch = find_first_mismatch(cx, pat);
if let Some((span, mutability, level)) = maybe_mismatch {
span_lint_and_help(
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
span_lint_and_then(
cx,
PATTERN_TYPE_MISMATCH,
span,
"type of pattern does not match the expression type",
None,
format!(
"{}explicitly match against a `{}` pattern and adjust the enclosed variable bindings",
match (deref_possible, level) {
(DerefPossible::Possible, Level::Top) => "use `*` to dereference the match expression or ",
_ => "",
},
match mutability {
Mutability::Mut => "&mut _",
Mutability::Not => "&_",
},
),
|diag| {
diag.help(format!(
"{}explicitly match against a `{}` pattern and adjust the enclosed variable bindings",
match (deref_possible, level) {
(DerefPossible::Possible, Level::Top) => "use `*` to dereference the match expression or ",
_ => "",
},
match mutability {
Mutability::Mut => "&mut _",
Mutability::Not => "&_",
},
));
},
);
true
} else {

View file

@ -1,4 +1,4 @@
use clippy_utils::diagnostics::span_lint_and_help;
use clippy_utils::diagnostics::span_lint_and_then;
use rustc_ast::ast::{Item, ItemKind, VisibilityKind};
use rustc_lint::{EarlyContext, EarlyLintPass};
use rustc_session::declare_lint_pass;
@ -42,14 +42,10 @@ impl EarlyLintPass for PubUse {
if let ItemKind::Use(_) = item.kind
&& let VisibilityKind::Public = item.vis.kind
{
span_lint_and_help(
cx,
PUB_USE,
item.span,
"using `pub use`",
None,
"move the exported item to a public module instead",
);
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
span_lint_and_then(cx, PUB_USE, item.span, "using `pub use`", |diag| {
diag.help("move the exported item to a public module instead");
});
}
}
}

View file

@ -1,4 +1,4 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::is_lint_allowed;
use clippy_utils::macros::span_is_local;
use clippy_utils::source::snippet;
@ -105,45 +105,51 @@ fn check_str(cx: &LateContext<'_>, span: Span, id: HirId) {
let string = snippet(cx, span, "");
if string.chars().any(|c| ['\u{200B}', '\u{ad}', '\u{2060}'].contains(&c)) {
span_lint_and_sugg(
cx,
INVISIBLE_CHARACTERS,
span,
"invisible character detected",
"consider replacing the string with",
string
.replace('\u{200B}', "\\u{200B}")
.replace('\u{ad}', "\\u{AD}")
.replace('\u{2060}', "\\u{2060}"),
Applicability::MachineApplicable,
);
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
span_lint_and_then(cx, INVISIBLE_CHARACTERS, span, "invisible character detected", |diag| {
diag.span_suggestion(
span,
"consider replacing the string with",
string
.replace('\u{200B}', "\\u{200B}")
.replace('\u{ad}', "\\u{AD}")
.replace('\u{2060}', "\\u{2060}"),
Applicability::MachineApplicable,
);
});
}
if string.chars().any(|c| c as u32 > 0x7F) {
span_lint_and_sugg(
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
span_lint_and_then(
cx,
NON_ASCII_LITERAL,
span,
"literal non-ASCII character detected",
"consider replacing the string with",
if is_lint_allowed(cx, UNICODE_NOT_NFC, id) {
escape(string.chars())
} else {
escape(string.nfc())
|diag| {
diag.span_suggestion(
span,
"consider replacing the string with",
if is_lint_allowed(cx, UNICODE_NOT_NFC, id) {
escape(string.chars())
} else {
escape(string.nfc())
},
Applicability::MachineApplicable,
);
},
Applicability::MachineApplicable,
);
}
if is_lint_allowed(cx, NON_ASCII_LITERAL, id) && string.chars().zip(string.nfc()).any(|(a, b)| a != b) {
span_lint_and_sugg(
cx,
UNICODE_NOT_NFC,
span,
"non-NFC Unicode sequence detected",
"consider replacing the string with",
string.nfc().collect::<String>(),
Applicability::MachineApplicable,
);
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
span_lint_and_then(cx, UNICODE_NOT_NFC, span, "non-NFC Unicode sequence detected", |diag| {
diag.span_suggestion(
span,
"consider replacing the string with",
string.nfc().collect::<String>(),
Applicability::MachineApplicable,
);
});
}
}

View file

@ -1,4 +1,4 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::source::snippet_opt;
use rustc_ast::ast::{Item, VisibilityKind};
use rustc_errors::Applicability;
@ -85,14 +85,19 @@ impl EarlyLintPass for Visibility {
if **path == kw::SelfLower
&& let Some(false) = is_from_proc_macro(cx, item.vis.span)
{
span_lint_and_sugg(
span_lint_and_then(
cx,
NEEDLESS_PUB_SELF,
item.vis.span,
format!("unnecessary `pub({}self)`", if *shorthand { "" } else { "in " }),
"remove it",
String::new(),
Applicability::MachineApplicable,
|diag| {
diag.span_suggestion_hidden(
item.vis.span,
"remove it",
String::new(),
Applicability::MachineApplicable,
);
},
);
}
@ -101,14 +106,20 @@ impl EarlyLintPass for Visibility {
&& let [.., last] = &*path.segments
&& let Some(false) = is_from_proc_macro(cx, item.vis.span)
{
span_lint_and_sugg(
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
span_lint_and_then(
cx,
PUB_WITHOUT_SHORTHAND,
item.vis.span,
"usage of `pub` with `in`",
"remove it",
format!("pub({})", last.ident),
Applicability::MachineApplicable,
|diag| {
diag.span_suggestion(
item.vis.span,
"remove it",
format!("pub({})", last.ident),
Applicability::MachineApplicable,
);
},
);
}
@ -116,14 +127,20 @@ impl EarlyLintPass for Visibility {
&& let [.., last] = &*path.segments
&& let Some(false) = is_from_proc_macro(cx, item.vis.span)
{
span_lint_and_sugg(
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
span_lint_and_then(
cx,
PUB_WITH_SHORTHAND,
item.vis.span,
"usage of `pub` without `in`",
"add it",
format!("pub(in {})", last.ident),
Applicability::MachineApplicable,
|diag| {
diag.span_suggestion(
item.vis.span,
"add it",
format!("pub(in {})", last.ident),
Applicability::MachineApplicable,
);
},
);
}
}

View file

@ -2,22 +2,27 @@ error: unnecessary `pub(self)`
--> tests/ui/needless_pub_self.rs:13:1
|
LL | pub(self) fn a() {}
| ^^^^^^^^^ help: remove it
| ^^^^^^^^^
|
= note: `-D clippy::needless-pub-self` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::needless_pub_self)]`
= help: remove it
error: unnecessary `pub(in self)`
--> tests/ui/needless_pub_self.rs:14:1
|
LL | pub(in self) fn b() {}
| ^^^^^^^^^^^^ help: remove it
| ^^^^^^^^^^^^
|
= help: remove it
error: unnecessary `pub(self)`
--> tests/ui/needless_pub_self.rs:20:5
|
LL | pub(self) fn f() {}
| ^^^^^^^^^ help: remove it
| ^^^^^^^^^
|
= help: remove it
error: aborting due to 3 previous errors