-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Add disabled-in-tests
configuration to disable arbitrary lints in tests
#15600
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
base: master
Are you sure you want to change the base?
Conversation
They work quite differently as currently implemented though. To me this seems like the wrong thing to check for, and this type of check has also lead to some confusion in the past when combining For example, if I have a crate with fn foo(opt: Option<i32>) {
#[expect(clippy::unwrap_used)]
opt.unwrap();
} and set |
@y21 Thanks for your prompt response. Can you see anything else wrong with this approach besides breaking One way to fix the But if this PR's approach breaks more than just |
I pushed some commits and changed the PR to draft in the hope of getting your feedback. Suppose I could get the changes in this post-expect-filter branch merged into rustc. Do you think then that the present PR might be considered for merger into Clippy? To be more specific, should I pursue getting that branch merged into rustc? Please note that I added a test that very closely resembles #15600 (comment): https://github.com/rust-lang/rust-clippy/blob/a9e61900d0f9b30e0c823e165c5f9bf4cdc4ad92/tests/ui-toml/disabled_in_tests_expect/expect.rs |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this needs the changes in rustc. If you want to manually fulfill an #[expect]
without showing it (I assume that's what it's for, correct?), we already do that in a bunch of places. You can look at how clippy_utils::fulfill_or_allowed
is implemented, it uses cx.fulfill_expectation(..)
.
It would technically fix the case I'd mentioned, but checking --test
still seems fundamentally like the wrong thing to me IMO. There won't be a false "unfulfilled expectation" warning like before, but now we won't get any warnings for the snippet in #15600 (comment) when removing the #[expect]
and run with --test
, even though it's not test code.
I still think the correct thing would be to not check for a test session but where the expression being linted actually is located. What do you think? Maybe this is something worth discussing on Zulip on what the general expectation is on how exactly this type of configuration would be used. Perhaps people do think it's fine that "disabled-in-tests" really means "disable in test sessions" as is implemented, instead of "disabled in test code" (what e.g. allow-unwrap-in-tests
does).
The current behavior of disabled-in-tests = ["unwrap_used"]
is also identical to #![cfg_attr(test, allow(clippy::unwrap_used)]
at the crate root, right?
src/driver.rs
Outdated
if sess.is_test_crate() { | ||
return FxHashSet::default(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For my own understanding, shouldn't this condition be inverted? The idea AFAIK is to essentially ignore disabled-in-tests
and return an empty set when not in a test crate, that is, we don't want to register this expect hook when not in a test crate, right? Unfortunately I don't have an easy way to test this branch atm since it's based on a modified rustc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oof. You're right.
Thanks. I wasn't aware of
That's correct. But I think that's fine, because one will get warnings for the non-test code when one builds without Just to be explicit:
I disagree. The current approach of searching for Another reason to use And to state the obvious, merging this PR wouldn't mean that
I will start a conversation on Zulip, but I would be fine with changing the name if you think
Ignoring things like |
Zulip thread (for my own reference): https://rust-lang.zulipchat.com/#narrow/channel/257328-clippy/topic/.60disabled-in-tests.60.20configuration.20.28PR.2015600.29 |
Adds a catch-all configuration to disable lints in tests, so that one-offs like #8802 are not needed.
To illustrate, the following two clippy.toml files have essentially the same effect:
The approach is to store a disabled lints set in
clippy_utils::diagnostics
. When a lint is in the set and--test
is passed to rustc, diagnostics are not emitted for the lint.Two other approaches I tried were:
unwrap_used
is in themethods::Methods
pass. So not registering that pass causesexpect_used
,should_implement_trait
, etc. to be disabled as well.declared_lints::LINTS
. This works but causes rustc to to produce "unknown lint" warnings on attributes like#[allow(unwrap_used)]
.The present PR's approach is also much simpler than the two just described. I could share code for the other approaches if desired, though.
I expect this PR to be subject to the feature freeze.
changelog: Add
disabled-in-tests
configuration to disable arbitrary lints in tests