Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

uninlined_format_args cannot be "allowed" in Cargo.toml #12161

Closed
szabgab opened this issue Jan 17, 2024 · 5 comments
Closed

uninlined_format_args cannot be "allowed" in Cargo.toml #12161

szabgab opened this issue Jan 17, 2024 · 5 comments
Labels
C-bug Category: Clippy is not doing the correct thing

Comments

@szabgab
Copy link

szabgab commented Jan 17, 2024

Summary

Given this code:

fn main() {
    let name = "Rust";
    let _x = 1;
    println!("Hello, {}!", name);
}

I turned on pedantic in Cargo.toml and then wanted to turn off (allow) the individual lints so I can fix them one-by-one.

I could turn off the no_effect_underscore_binding lint (and in a bigger application every other lint that was complaining), but not the uninlined_format_args:

[lints.clippy]
pedantic = "deny"
uninlined_format_args = "allow"
no_effect_underscore_binding = "allow"

When running on the command-line (with or without the section in Cargo.toml then everything is fine.

cargo clippy -- -Dclippy::pedantic -Aclippy::uninlined_format_args -Aclippy::no_effect_underscore_binding

Reproducer

No response

Version

cargo clippy --version
clippy 0.1.75 (82e1608 2023-12-21)


rustc -Vv
rustc 1.75.0 (82e1608df 2023-12-21)
binary: rustc
commit-hash: 82e1608dfa6e0b5569232559e3d385fea5a93112
commit-date: 2023-12-21
host: x86_64-unknown-linux-gnu
release: 1.75.0
LLVM version: 17.0.6


### Additional Labels

_No response_
@szabgab szabgab added the C-bug Category: Clippy is not doing the correct thing label Jan 17, 2024
@y21
Copy link
Member

y21 commented Jan 17, 2024

This happens because of how lint tables are implemented in Cargo.

See #12093 for what's happening here. The TL;DR is that if you don't specify a priority for a lint entry in the table, then a default priority of 0 is used.

Cargo sorts lint entries with the same priority in an arbitrary order, and at the moment it happens to sort them in reverse alphabetical order.
Here, uninlined_format_args > pedantic, so pedantic is placed after it and overrides the allow for that lint. You can see this happen with any lint in the pedantic group that comes after pedantic, such as struct_field_names. This is also why no_effect_underscore_binding worked.

You can fix it by giving pedantic a negative priority

pedantic = { priority = -1, level = "deny" }

That way the default priority 0 will override it.
You can of course also give specific lints a priority higher than 0.

@szabgab
Copy link
Author

szabgab commented Jan 17, 2024

Oh. that's interesting and strange.

Now I tried and if I put the flags on the command line in a different order where pedantic is at the end then both of the other two lints will complain as pedantic overrides the "allow" I just added.

cargo clippy -- -Aclippy::uninlined_format_args -Aclippy::no_effect_underscore_binding -Dclippy::pedantic

Thanks for the quick reply. I guess I can close this issue then.

I've added it to my explanation

@szabgab szabgab closed this as completed Jan 17, 2024
@y21
Copy link
Member

y21 commented Jan 17, 2024

Yeah, I agree this behavior is not ideal. The RFC mentions that the behavior is explicitly chosen this way because it's unlikely to do what the user wants to and that it would raise awareness:

On initial release, the sort will likely be reverse alphabetical which would cause all to be last, making it unlikely to do what the user wants which would raise awareness of the need for setting priority for all groups.

FWIW, this isn't specific to clippy. The linting framework entirely exists in rustc and the lint table is processed (and turned into command line arguments) by cargo. Clippy only extends the rust compiler with additional lints, so there is not much that can be done from the clippy side to fix this (ie builtin rustc lints have the same issue).
But there is an open pull request that adds a lint, which would have caught this :) #11832

The upstream issue over at cargo is rust-lang/cargo#12918

@y21
Copy link
Member

y21 commented Jan 17, 2024

@szabgab also FYI (reading the post you linked now), you can run cargo clippy --fix to have it autofix a bunch of lints (those lints where clippy is confident enough that the suggestion will do what you expect and won't result in errors). must_use_candidate for example has one such suggestion.

@szabgab
Copy link
Author

szabgab commented Jan 18, 2024

@y21 thanks. I am aware of --fix but I am still a bit scared of it and besides fixing them manually will help me understand and learn the idea behind each lint.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing
Projects
None yet
Development

No branches or pull requests

2 participants