Skip to content

An unused generic parameter should be a warning at the definition site. #34396

Open
@pnkfelix

Description

@pnkfelix

Consider the following code:

fn foo<F>(x: i32) -> i32 { x }

fn bar<'a>(x: i32) -> i32 { x }

fn main() {
    bar(1);
    bar(2);
    foo::<f64>(3);
    foo(4);
    foo(5);
}

Currently, this causes a fatal compiler error at the invocation of foo(4) on line 9, of the form:

error: unable to infer enough type information about `_`; type annotations or generic parameter binding required [--explain E0282]
 --> <anon>:9:5
  |>
9 |>     foo(4);
  |>     ^^^

error: aborting due to previous error

The only way to ever successfully invoke foo is to feed it an explicit type (via the ::<_> syntax as illustrated on line 8).

I suspect in many cases of this error, the actual problem is that the definition of fn foo (and also fn bar) are faulty: since F and 'a are never referenced in the signature nor in the function body, they are irrelevant and should cause a warning via the compiler's linting system.

(A separate issue is that one cannot actually instantiate the 'a parameter to fn bar via the ::<_> syntax, so it seems that its formal lifetime parameter is truly unusable. But things may not remain that way forever, so I think its best to lump both of these cases into the same category.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-feature-requestCategory: A feature request, i.e: not implemented / a PR.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions