From d5a4c520743ed6d216011bd897f6d43ede2559bd Mon Sep 17 00:00:00 2001 From: Viktar Makouski Date: Wed, 16 Apr 2025 16:23:27 +0300 Subject: [PATCH 1/2] something nice --- clippy_lints/src/mut_reference.rs | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/clippy_lints/src/mut_reference.rs b/clippy_lints/src/mut_reference.rs index 2fd1049f42e1..20661da9cbc6 100644 --- a/clippy_lints/src/mut_reference.rs +++ b/clippy_lints/src/mut_reference.rs @@ -1,8 +1,15 @@ -use clippy_utils::diagnostics::span_lint; -use rustc_hir::{BorrowKind, Expr, ExprKind, Mutability}; +use clippy_utils::diagnostics::span_lint_and_sugg; +use clippy_utils::source::snippet; +use rustc_errors::Applicability; +use rustc_hir::{ + AssocItemKind, BorrowKind, Expr, ExprKind, FieldDef, HirId, ImplItemRef, IsAuto, Item, ItemKind, Mod, Mutability, + QPath, TraitItemRef, TyKind, Variant, VariantData, +}; use rustc_lint::{LateContext, LateLintPass}; -use rustc_middle::ty::{self, Ty}; +use rustc_middle::ty::print::with_forced_trimmed_paths; +use rustc_middle::ty::{self, GenericArgKind, Ty}; use rustc_session::declare_lint_pass; +use rustc_span::symbol::sym; use std::iter; declare_clippy_lint! { @@ -86,11 +93,16 @@ fn check_arguments<'tcx>( match parameter.kind() { ty::Ref(_, _, Mutability::Not) | ty::RawPtr(_, Mutability::Not) => { if let ExprKind::AddrOf(BorrowKind::Ref, Mutability::Mut, _) = argument.kind { - span_lint( + let snippet = snippet(cx, argument.span, ".."); + let trimmed_snippet = &snippet[5..]; + span_lint_and_sugg( cx, UNNECESSARY_MUT_PASSED, argument.span, format!("the {fn_kind} `{name}` doesn't need a mutable reference"), + "try", + format!("&{trimmed_snippet}"), + Applicability::MachineApplicable, ); } }, From 6ab0b14ef83efc0f1281773373df2504408ebdb8 Mon Sep 17 00:00:00 2001 From: Viktar Makouski Date: Wed, 16 Apr 2025 16:31:38 +0300 Subject: [PATCH 2/2] feat: make unnecessary mut autofixable --- clippy_lints/src/mut_reference.rs | 9 ++------- tests/ui/mut_reference.stderr | 6 +++--- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/clippy_lints/src/mut_reference.rs b/clippy_lints/src/mut_reference.rs index 20661da9cbc6..b63f65e0f1dc 100644 --- a/clippy_lints/src/mut_reference.rs +++ b/clippy_lints/src/mut_reference.rs @@ -1,15 +1,10 @@ use clippy_utils::diagnostics::span_lint_and_sugg; use clippy_utils::source::snippet; use rustc_errors::Applicability; -use rustc_hir::{ - AssocItemKind, BorrowKind, Expr, ExprKind, FieldDef, HirId, ImplItemRef, IsAuto, Item, ItemKind, Mod, Mutability, - QPath, TraitItemRef, TyKind, Variant, VariantData, -}; +use rustc_hir::{BorrowKind, Expr, ExprKind, Mutability}; use rustc_lint::{LateContext, LateLintPass}; -use rustc_middle::ty::print::with_forced_trimmed_paths; -use rustc_middle::ty::{self, GenericArgKind, Ty}; +use rustc_middle::ty::{self, Ty}; use rustc_session::declare_lint_pass; -use rustc_span::symbol::sym; use std::iter; declare_clippy_lint! { diff --git a/tests/ui/mut_reference.stderr b/tests/ui/mut_reference.stderr index 474221329c25..359428f0542a 100644 --- a/tests/ui/mut_reference.stderr +++ b/tests/ui/mut_reference.stderr @@ -2,7 +2,7 @@ error: the function `takes_an_immutable_reference` doesn't need a mutable refere --> tests/ui/mut_reference.rs:30:34 | LL | takes_an_immutable_reference(&mut 42); - | ^^^^^^^ + | ^^^^^^^ help: try: `&42` | = note: `-D clippy::unnecessary-mut-passed` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(clippy::unnecessary_mut_passed)]` @@ -11,13 +11,13 @@ error: the function `as_ptr` doesn't need a mutable reference --> tests/ui/mut_reference.rs:34:12 | LL | as_ptr(&mut 42); - | ^^^^^^^ + | ^^^^^^^ help: try: `&42` error: the method `takes_an_immutable_reference` doesn't need a mutable reference --> tests/ui/mut_reference.rs:39:44 | LL | my_struct.takes_an_immutable_reference(&mut 42); - | ^^^^^^^ + | ^^^^^^^ help: try: `&42` error: aborting due to 3 previous errors