Skip to content

Allow unused arguments for trait methods with default implementations #91074

@not-my-profile

Description

@not-my-profile

Given the following code:

pub trait MyTrait {
    fn func(&self, arg: &str) -> bool  {
        false
    }
}

The current output is:

warning: unused variable: `arg`
 --> src/lib.rs:2:20
  |
2 |     fn func(&self, arg: &str) -> bool  {
  |                    ^^^ help: if this is intentional, prefix it with an underscore: `_arg`
  |
  = note: `#[warn(unused_variables)]` on by default

I think rustc should not output this warning in this case since the parameter name is part of the public API. The parameter names show up in the documentation generated by rustdoc and having underscores there looks very weird.

Yes I am aware that I can suppress the warning by doing:

pub trait MyTrait {
    fn func(&self, #[allow(unused_variables)] arg: &str) -> bool {
        false
    }
}

However I would argue that this makes the code quite unreadable (and moving the attribute before the function would mean no more warnings for actually unused variables within the default implementation).

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions