Skip to content

use_self does not work with const parameters #16164

@matthiaskrgr

Description

@matthiaskrgr

Using the following flags

--force-warn clippy::use-self

this code:

// Regression test for the ICE described in #86820.

#![allow(unused, dead_code)]
use std::ops::BitAnd;

const C: fn() = || is_set();
fn is_set() {
    0xffu8.bit::<0>();
}

trait Bits {
    fn bit<const I: u8>(self) -> bool;
}

impl Bits for u8 {
    fn bit<const I: u8>(self) -> bool {
        //~^ ERROR: method `bit` has an incompatible generic parameter for trait `Bits` [E0053]
        let i = 1 << I;
        let mask = u8::from(i);
        mask & self == mask
    }
}

fn main() {}

caused the following diagnostics:

    Checking _af1ccf11f2d169cbd7b7063d072b57da939c37b1 v0.1.0 (/tmp/icemaker_global_tempdir.fzTw1TOrsbiL/icemaker_clippyfix_tempdir.0y2xUIR3TJMR/_af1ccf11f2d169cbd7b7063d072b57da939c37b1)
warning: unnecessary structure name repetition
  --> src/main.rs:16:21
   |
16 |     fn bit<const I: u8>(self) -> bool {
   |                     ^^ help: use the applicable keyword: `Self`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#use_self
   = note: requested on the command line with `--force-warn clippy::use-self`

warning: unnecessary structure name repetition
  --> src/main.rs:19:20
   |
19 |         let mask = u8::from(i);
   |                    ^^ help: use the applicable keyword: `Self`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#use_self

warning: `_af1ccf11f2d169cbd7b7063d072b57da939c37b1` (bin "_af1ccf11f2d169cbd7b7063d072b57da939c37b1") generated 2 warnings (run `cargo clippy --fix --bin "_af1ccf11f2d169cbd7b7063d072b57da939c37b1" -p _af1ccf11f2d169cbd7b7063d072b57da939c37b1` to apply 2 suggestions)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.22s

However after applying these diagnostics, the resulting code:

// Regression test for the ICE described in #86820.

#![allow(unused, dead_code)]
use std::ops::BitAnd;

const C: fn() = || is_set();
fn is_set() {
    0xffu8.bit::<0>();
}

trait Bits {
    fn bit<const I: u8>(self) -> bool;
}

impl Bits for u8 {
    fn bit<const I: Self>(self) -> bool {
        //~^ ERROR: method `bit` has an incompatible generic parameter for trait `Bits` [E0053]
        let i = 1 << I;
        let mask = Self::from(i);
        mask & self == mask
    }
}

fn main() {}

no longer compiled:

    Checking _af1ccf11f2d169cbd7b7063d072b57da939c37b1 v0.1.0 (/tmp/icemaker_global_tempdir.fzTw1TOrsbiL/icemaker_clippyfix_tempdir.0y2xUIR3TJMR/_af1ccf11f2d169cbd7b7063d072b57da939c37b1)
error[E0770]: the type of const parameters must not depend on other generic parameters
  --> src/main.rs:16:21
   |
16 |     fn bit<const I: Self>(self) -> bool {
   |                     ^^^^ the type must not depend on the parameter `Self`

error[E0053]: method `bit` has an incompatible generic parameter for trait `Bits`
  --> src/main.rs:16:12
   |
11 | trait Bits {
   |       ----
12 |     fn bit<const I: u8>(self) -> bool;
   |            ----------- expected const parameter of type `u8`
...
15 | impl Bits for u8 {
   | ----------------
16 |     fn bit<const I: Self>(self) -> bool {
   |            ^^^^^^^^^^^^^ found const parameter of type `{type error}`

Some errors have detailed explanations: E0053, E0770.
For more information about an error, try `rustc --explain E0053`.
error: could not compile `_af1ccf11f2d169cbd7b7063d072b57da939c37b1` (bin "_af1ccf11f2d169cbd7b7063d072b57da939c37b1" test) due to 2 previous errors
warning: build failed, waiting for other jobs to finish...
error: could not compile `_af1ccf11f2d169cbd7b7063d072b57da939c37b1` (bin "_af1ccf11f2d169cbd7b7063d072b57da939c37b1") due to 2 previous errors

Version:

rustc 1.93.0-nightly (f40a70d2b 2025-11-30)
binary: rustc
commit-hash: f40a70d2bcd830a4f1f8c7ca1a7f93f1d9d703d6
commit-date: 2025-11-30
host: x86_64-unknown-linux-gnu
release: 1.93.0-nightly
LLVM version: 21.1.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    I-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions