Skip to content

Commit

Permalink
Make restriction lint's use span_lint_and_then (n -> p 75/116)
Browse files Browse the repository at this point in the history
  • Loading branch information
xFrednet committed Jul 21, 2024
1 parent 497dbe8 commit 2d5fb9c
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 85 deletions.
26 changes: 9 additions & 17 deletions clippy_lints/src/partial_pub_fields.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
}
}
Expand Down
30 changes: 16 additions & 14 deletions clippy_lints/src/pattern_type_mismatch.rs
Original file line number Diff line number Diff line change
@@ -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,
};
Expand Down Expand Up @@ -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 {
Expand Down
14 changes: 5 additions & 9 deletions clippy_lints/src/pub_use.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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");
});
}
}
}
64 changes: 35 additions & 29 deletions clippy_lints/src/unicode.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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,
);
});
}
}
43 changes: 30 additions & 13 deletions clippy_lints/src/visibility.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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,
);
},
);
}

Expand All @@ -101,29 +106,41 @@ 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,
);
},
);
}

if *shorthand
&& 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,
);
},
);
}
}
Expand Down
11 changes: 8 additions & 3 deletions tests/ui/needless_pub_self.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 2d5fb9c

Please sign in to comment.