diff options
| author | bors <bors@rust-lang.org> | 2024-08-05 13:33:16 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2024-08-05 13:33:16 +0000 |
| commit | c082bc2cb85313901ed3565fcd285592ed93df0f (patch) | |
| tree | fc37b0b084729b9d6b93ec704d6e36f51b741a8e /clippy_lints/src/methods | |
| parent | e17d254e2bd3fc78bbff021171813cc0e9424b97 (diff) | |
| parent | 053210424727ff279515f4b9a7f0cf6e5632acf3 (diff) | |
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`
Diffstat (limited to 'clippy_lints/src/methods')
| -rw-r--r-- | clippy_lints/src/methods/clone_on_ref_ptr.rs | 23 | ||||
| -rw-r--r-- | clippy_lints/src/methods/filetype_is_file.rs | 9 | ||||
| -rw-r--r-- | clippy_lints/src/methods/get_unwrap.rs | 44 |
3 files changed, 45 insertions, 31 deletions
diff --git a/clippy_lints/src/methods/clone_on_ref_ptr.rs b/clippy_lints/src/methods/clone_on_ref_ptr.rs index 926bd06bacb..e0826b53004 100644 --- a/clippy_lints/src/methods/clone_on_ref_ptr.rs +++ b/clippy_lints/src/methods/clone_on_ref_ptr.rs @@ -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, + ); + }, ); } } diff --git a/clippy_lints/src/methods/filetype_is_file.rs b/clippy_lints/src/methods/filetype_is_file.rs index eab536b88a5..2ab0401947c 100644 --- a/clippy_lints/src/methods/filetype_is_file.rs +++ b/clippy_lints/src/methods/filetype_is_file.rs @@ -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")); + }); } diff --git a/clippy_lints/src/methods/get_unwrap.rs b/clippy_lints/src/methods/get_unwrap.rs index 455274a4428..c6285c87a26 100644 --- a/clippy_lints/src/methods/get_unwrap.rs +++ b/clippy_lints/src/methods/get_unwrap.rs @@ -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, + ); + }, ); } |
