rust-lang / rust-lang/rust-clippy

some #[allow(...)] outer attributes do not apply to just_underscores_and_digits lint

Open
#12,220 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-false-positive
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Summary

Clippy produces false warnings for the following code. The code is not expected to produce warnings as the #[allow(...)] outer attribute should apply to the following line

fn main() {}

fn _just_underscores_and_digits_0() {
    #[allow(clippy::just_underscores_and_digits)]
    let _1 = 1; // warning
}

fn _just_underscores_and_digits_1() {
    #[allow(warnings)]
    let _1 = 1; // warning
}

These examples work fine

#[allow(clippy::just_underscores_and_digits)]
fn _just_underscores_and_digits_2() {
    let _1 = 1;
}

fn _just_underscores_and_digits_3() {
    #![allow(clippy::just_underscores_and_digits)]
    let _1 = 1;
}

Try it in the rust playground

just_underscores_and_digits can be found in clippy_lints/src/non_expressive_names.rs, which houses 2 other related lints (many_single_char_names and similar_names) .

many_single_char_names shares the same problem as just_underscores_and_digits (run with -W clippy::many_single_char_names since the lint group is pedantic)

fn _many_single_char_names_0() {
    #[allow(clippy::many_single_char_names)]
    let (a, b, c, d, e, f, g) = (1, 2, 3, 4, 5, 6, 7); // warning
}

fn _many_single_char_names_1() {
    #[allow(warnings)]
    let (a, b, c, d, e, f, g) = (1, 2, 3, 4, 5, 6, 7); // warning
}

The remaining lint (similar_names) seems to be a false negative (use -W clippy::similar_names)

// this should warn but does not
fn _similar_names() {
    let checked_exp = 1;
    let checked_expr = 1;
}

I've skimmed through the code but frankly the more I look the more I wonder if it works at all:

  • similar_names is not working as levenstein_not_1 doesn't seem to work as expected (try assert!(!levenstein_not_1("checked_exp", "checked_expr")))
  • just_underscores_and_digits and many_single_char_names AST walks share the same visitor as similar_names (SimilarNamesLocalVisitor). The custom visit_local implementation in the visitor seems to prevent outer attributes from applying correctly

To be clear, I don't use these lints so they don't bother me, but I did spend good time trying to understand why #[allow(...)] wasn't working. Perhaps if we can determine that these lints are somewhat unused deprecating them can also be an option

Lint Name

just_underscores_and_digits, many_single_char_names, similar_names

Version
rustc 1.77.0-nightly (7ffc697ce 2024-01-24)
binary: rustc
commit-hash: 7ffc697ce10f19447c0ce338428ae4b9bc0c041c
commit-date: 2024-01-24
host: x86_64-unknown-linux-gnu
release: 1.77.0-nightly
LLVM version: 17.0.6

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in clippy_lints/src/non_expressive_names.rs and inspect SimilarNamesLocalVisitor, especially its custom visit_local implementation. Reproduce the reported examples for just_underscores_and_digits, many_single_char_names, and similar_names, then verify that outer allow attributes behave correctly and that the similar_names result matches the issue's expectations.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.