sugg on ty

This commit is contained in:
Ada Alakbarova 2025-08-04 00:12:47 +02:00
parent 269870a5f9
commit 729d92cf04
No known key found for this signature in database
3 changed files with 21 additions and 15 deletions

View file

@ -1,5 +1,6 @@
use clippy_utils::diagnostics::{span_lint, span_lint_hir_and_then};
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_hir_and_then};
use clippy_utils::higher;
use clippy_utils::source::snippet_with_applicability;
use clippy_utils::sugg::Sugg;
use rustc_errors::Applicability;
use rustc_hir::{self as hir, AmbigArg, intravisit};
@ -37,15 +38,20 @@ impl<'tcx> LateLintPass<'tcx> for MutMut {
fn check_ty(&mut self, cx: &LateContext<'tcx>, ty: &'tcx hir::Ty<'_, AmbigArg>) {
if let hir::TyKind::Ref(_, mty) = ty.kind
&& mty.mutbl == hir::Mutability::Mut
&& let hir::TyKind::Ref(_, mty) = mty.ty.kind
&& mty.mutbl == hir::Mutability::Mut
&& let hir::TyKind::Ref(_, mty2) = mty.ty.kind
&& mty2.mutbl == hir::Mutability::Mut
&& !ty.span.in_external_macro(cx.sess().source_map())
{
span_lint(
let mut applicability = Applicability::MaybeIncorrect;
let sugg = snippet_with_applicability(cx.sess(), mty.ty.span, "..", &mut applicability);
span_lint_and_sugg(
cx,
MUT_MUT,
ty.span,
"generally you want to avoid `&mut &mut _` if possible",
"a type of form `&mut &mut _`",
"remove the extra `&mut`",
sugg.to_string(),
applicability,
);
}
}

View file

@ -1,8 +1,8 @@
error: generally you want to avoid `&mut &mut _` if possible
error: a type of form `&mut &mut _`
--> tests/ui/mut_mut.rs:15:11
|
LL | fn fun(x: &mut &mut u32) {
| ^^^^^^^^^^^^^
| ^^^^^^^^^^^^^ help: remove the extra `&mut`: `&mut u32`
|
= note: `-D clippy::mut-mut` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::mut_mut)]`
@ -25,11 +25,11 @@ error: an expression of form `&mut &mut _`
LL | let y: &mut &mut u32 = &mut &mut 2;
| ^^^^^^^^^^^ help: remove the extra `&mut`: `&mut 2`
error: generally you want to avoid `&mut &mut _` if possible
error: a type of form `&mut &mut _`
--> tests/ui/mut_mut.rs:40:16
|
LL | let y: &mut &mut u32 = &mut &mut 2;
| ^^^^^^^^^^^^^
| ^^^^^^^^^^^^^ help: remove the extra `&mut`: `&mut u32`
error: an expression of form `&mut &mut _`
--> tests/ui/mut_mut.rs:46:37
@ -37,17 +37,17 @@ error: an expression of form `&mut &mut _`
LL | let y: &mut &mut &mut u32 = &mut &mut &mut 2;
| ^^^^^^^^^^^^^^^^ help: remove the extra `&mut`: `&mut &mut 2`
error: generally you want to avoid `&mut &mut _` if possible
error: a type of form `&mut &mut _`
--> tests/ui/mut_mut.rs:46:16
|
LL | let y: &mut &mut &mut u32 = &mut &mut &mut 2;
| ^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^ help: remove the extra `&mut`: `&mut &mut u32`
error: generally you want to avoid `&mut &mut _` if possible
error: a type of form `&mut &mut _`
--> tests/ui/mut_mut.rs:46:21
|
LL | let y: &mut &mut &mut u32 = &mut &mut &mut 2;
| ^^^^^^^^^^^^^
| ^^^^^^^^^^^^^ help: remove the extra `&mut`: `&mut u32`
error: aborting due to 8 previous errors

View file

@ -1,8 +1,8 @@
error: generally you want to avoid `&mut &mut _` if possible
error: a type of form `&mut &mut _`
--> tests/ui/mut_mut_unfixable.rs:9:11
|
LL | fn fun(x: &mut &mut u32) -> bool {
| ^^^^^^^^^^^^^
| ^^^^^^^^^^^^^ help: remove the extra `&mut`: `&mut u32`
|
= note: `-D clippy::mut-mut` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::mut_mut)]`