gccrs icon indicating copy to clipboard operation
gccrs copied to clipboard

Unused variable pass flags variables in derived trait functions

Open sakupan102 opened this issue 5 months ago • 1 comments

Summary

The unused variable analysis pass currently checks variables inside trait implementations generated via #[derive(...)]. However, since these functions are not written by the user and are automatically generated by the compiler, such warnings can be confusing and should likely be suppressed.

Reproducer

I tried this code:

#[lang = "sized"]
trait Sized {}

#[lang = "copy"]
trait Copy {}


#[lang = "structural_peq"]
trait StructuralPartialEq {}

#[lang = "eq"]
pub trait PartialEq<Rhs: ?Sized = Self> {
    fn eq(&self, other: &Rhs) -> bool;

    fn ne(&self, other: &Rhs) -> bool {
        !self.eq(other)
    }
}

#[derive(PartialEq, Copy)] // { dg-warning "unused name" }
struct Foo;

Does the code make use of any (1.49) nightly feature ?

  • [ ] Nightly

Godbolt link

No response

Actual behavior

<source>:29:3: warning: unused name 'other' [-Wunused-variable]
   29 | #[derive(PartialEq, Copy)] // { dg-warning "unused name" }
      |   ^~~~~~

Expected behavior

No response

GCC Version

dd7a656cb13f83cd32671b34865d0a4b265162c0

sakupan102 avatar Aug 01 '25 02:08 sakupan102

I'm currently working on implementing an unused variable checker on the HIR. I'll fix it once that's done.

sakupan102 avatar Aug 01 '25 02:08 sakupan102