Skip to content

Giving a trait method a default implementation can trigger unused_variables warnings #33875

Closed
@chris-morgan

Description

@chris-morgan
trait X {
    fn foo(bar: Bar);
}

Fine, no warnings.

But give it a default implementation that doesn’t use all those arguments (a perfectly plausible and reasonable situation):

trait X {
    fn foo(bar: Bar) { }
}

A warning:

<anon>:2:12: 2:15 warning: unused variable: `bar`, #[warn(unused_variables)] on by default
<anon>:2     fn foo(bar: Bar) { }
                    ^~~

Arguments to a trait fn default implementation should be considered immune from unused variables warnings, because their names are a part of the API as it gets documented.

The alternatives are (a) prefixing the variable name with an underscore, which is ugly in generated API docs; and (b) tacking #[allow(unused_variables)] onto the fn foo, which then silences other legitimate unused variables warnings in the body of the function.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions