Auto merge of #13136 - xFrednet:07797-restriction-and-then, r=blyxyas

Make restriction lint's use `span_lint_and_then` (a -> e)

This migrates a few restriction lints to use `span_lint_and_then`. This change is motivated by https://github.com/rust-lang/rust-clippy/issues/7797.

I'm also interested if it will have an impact on performance. With some of these lints, like [`clippy::implicit_return`](https://rust-lang.github.io/rust-clippy/master/index.html#/implicit_return) I expect an impact, as it was previously creating a suggestion **for every implicit return** which is just wild.

I've also cleaned up some lint message. Mostly minor stuff. For example: suggestions with a longer message than `"try"` now use `SuggestionStyle::ShowAlways`

---

`@blyxyas` Could you benchmark this PR? I want to get all the numbers :3

---

This also crashed our new lintcheck CI with the following message:

> Error: $GITHUB_STEP_SUMMARY upload aborted, supports content up to a size of 1024k, got 46731k. For more information see: https://docs.github.com/actions/using-workflows/workflow-commands-for-github-actions#adding-a-markdown-summary

Which is just wild. Like, I've [tested the first 20 lints](https://github.com/xFrednet/rust-clippy/actions/runs/10027528172) and got like four changes and then this. 50 MB of changed lint messages o.O. Looks like I'll create a separate PR to fix that step ^^

---

cc: https://github.com/rust-lang/rust-clippy/issues/7797

changelog: none

r? `@blyxyas`
This commit is contained in:
bors 2024-08-05 13:33:16 +00:00
commit c082bc2cb8
34 changed files with 927 additions and 470 deletions

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_with_context;
use rustc_errors::Applicability;
use rustc_hir as hir;
@ -29,19 +29,22 @@ pub(super) fn check(
sym::RcWeak | sym::ArcWeak => "Weak",
_ => return,
};
// Sometimes unnecessary ::<_> after Rc/Arc/Weak
let mut app = Applicability::Unspecified;
let snippet = snippet_with_context(cx, receiver.span, expr.span.ctxt(), "..", &mut app).0;
span_lint_and_sugg(
span_lint_and_then(
cx,
CLONE_ON_REF_PTR,
expr.span,
"using `.clone()` on a ref-counted pointer",
"try",
format!("{caller_type}::<{}>::clone(&{snippet})", subst.type_at(0)),
app,
|diag| {
// Sometimes unnecessary ::<_> after Rc/Arc/Weak
let mut app = Applicability::Unspecified;
let snippet = snippet_with_context(cx, receiver.span, expr.span.ctxt(), "..", &mut app).0;
diag.span_suggestion(
expr.span,
"try",
format!("{caller_type}::<{}>::clone(&{snippet})", subst.type_at(0)),
app,
);
},
);
}
}

View file

@ -1,4 +1,4 @@
use clippy_utils::diagnostics::span_lint_and_help;
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::get_parent_expr;
use clippy_utils::ty::is_type_diagnostic_item;
use rustc_hir as hir;
@ -33,6 +33,9 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &hir::Expr
span = expr.span;
}
let lint_msg = format!("`{lint_unary}FileType::is_file()` only {verb} regular files");
let help_msg = format!("use `{help_unary}FileType::is_dir()` instead");
span_lint_and_help(cx, FILETYPE_IS_FILE, span, lint_msg, None, help_msg);
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
span_lint_and_then(cx, FILETYPE_IS_FILE, span, lint_msg, |diag| {
diag.help(format!("use `{help_unary}FileType::is_dir()` instead"));
});
}

View file

@ -1,5 +1,5 @@
use super::utils::derefs_to_slice;
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::get_parent_expr;
use clippy_utils::source::snippet_with_applicability;
use clippy_utils::ty::is_type_diagnostic_item;
@ -19,9 +19,7 @@ pub(super) fn check<'tcx>(
) {
// Note: we don't want to lint `get_mut().unwrap` for `HashMap` or `BTreeMap`,
// because they do not implement `IndexMut`
let mut applicability = Applicability::MachineApplicable;
let expr_ty = cx.typeck_results().expr_ty(recv);
let get_args_str = snippet_with_applicability(cx, get_arg.span, "..", &mut applicability);
let caller_type = if derefs_to_slice(cx, recv, expr_ty).is_some() {
"slice"
} else if is_type_diagnostic_item(cx, expr_ty, sym::Vec) {
@ -58,24 +56,34 @@ pub(super) fn check<'tcx>(
};
let mut_str = if is_mut { "_mut" } else { "" };
let borrow_str = if !needs_ref {
""
} else if is_mut {
"&mut "
} else {
"&"
};
span_lint_and_sugg(
span_lint_and_then(
cx,
GET_UNWRAP,
span,
format!("called `.get{mut_str}().unwrap()` on a {caller_type}. Using `[]` is more clear and more concise"),
"try",
format!(
"{borrow_str}{}[{get_args_str}]",
snippet_with_applicability(cx, recv.span, "..", &mut applicability)
),
applicability,
format!("called `.get{mut_str}().unwrap()` on a {caller_type}"),
|diag| {
let mut applicability = Applicability::MachineApplicable;
let get_args_str = snippet_with_applicability(cx, get_arg.span, "..", &mut applicability);
let borrow_str = if !needs_ref {
""
} else if is_mut {
"&mut "
} else {
"&"
};
diag.span_suggestion_with_style(
span,
"using `[]` is clearer and more concise",
format!(
"{borrow_str}{}[{get_args_str}]",
snippet_with_applicability(cx, recv.span, "..", &mut applicability)
),
applicability,
rustc_errors::SuggestionStyle::ShowAlways,
);
},
);
}