Skip to content

Inconsistent unused warning for local const inside unused methods (but not functions) #69016

Closed
@dbdr

Description

@dbdr
fn _unused1(x: i32) -> i32 {
    const F: i32 = 2;
    let g = 1;
    x * F + g
} 

pub struct Foo {}

impl Foo {
    fn _unused2(x: i32) -> i32 {
        const F: i32 = 2;
        let g = 1;
        x * F + g
    } 
}

(Playground)

There is a warning about F being unused in method _unused2, while there isn't about the same situation in function _unused1:

   Compiling playground v0.0.1 (/playground)
warning: constant item is never used: `F`
  --> src/lib.rs:11:9
   |
11 |         const F: i32 = 2;
   |         ^^^^^^^^^^^^^^^^^
   |
   = note: `#[warn(dead_code)]` on by default

    Finished dev [unoptimized + debuginfo] target(s) in 0.54s

Local variable g is also not reported as unused in either case. Unless I'm mistaken, F and g are similar in that they are local to the function/method. I would expect no warning for this code.

Note that this is different from #47133 because here the const is local, which I think makes the case more clear-cut.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions