-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Add assert(true) and assert(false) lints #3582
Conversation
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.
Generally looks good to me, but others might want to weigh in as well.
Error message changed to:
|
I would combine these lints into one. Maybe call it |
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.
Thanks for your contribution and welcome to Clippy! 🎉
This LGTM as a first implementation of this lint. Some easy and some advanced enhancement could be added to the lint:
- suggest
""
/"panic!()"
(easy) - also lint on constants (advanced)
- if it is a
assert!(false, "_")
call, suggestpanic!("_")
instead of justpanic!()
(advanced)
The easy one should be done in this PR, the advanced ones are just nice to have enhancement, that could be added later.
Please also add tests for assert!(true/false, "some message")
clippy_lints/src/assert_checks.rs
Outdated
then { | ||
match inner.node { | ||
LitKind::Bool(true) => { | ||
span_lint(cx, EXPLICIT_TRUE, e.span, |
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.
You can add a pretty simple suggestion to this lints:
true
=>sugg: ""
false
=>sugg: "panic!()"
You can use
rust-clippy/clippy_lints/src/utils/mod.rs
Lines 667 to 675 in 44ffda7
pub fn span_lint_and_sugg<'a, 'tcx: 'a, T: LintContext<'tcx>>( | |
cx: &'a T, | |
lint: &'static Lint, | |
sp: Span, | |
msg: &str, | |
help: &str, | |
sugg: String, | |
applicability: Applicability, | |
) { |
for this.
Thanks for your review @flip1995 👍 |
☔ The latest upstream changes (presumably #3558) made this pull request unmergeable. Please resolve the merge conflicts. |
ping from triage @Arkweid: Any updates on this? |
@flip1995 Ye, I am here. Just holidays in Russia until 9 January :) |
6a100eb
to
906b516
Compare
tests/ui/assertions_on_constants.rs
Outdated
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
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.
// run-rustfix | |
e.span, | ||
"assert!(false) should probably be replaced", | ||
"try", | ||
"panic!()".to_string(), |
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.
panic!()
or unreachable!()
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.
Work in progress. I pushed the current state of PR to test span_lint_and_sugg. Seems it not work as expected for assert!()
macro. Maybe I should replace it with span_lint_and_help
function?
@flip1995 changes pushed |
@@ -63,12 +53,18 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssertChecks { | |||
then { | |||
match inner.node { | |||
LitKind::Bool(true) => { | |||
span_lint(cx, EXPLICIT_TRUE, e.span, | |||
span_lint(cx, ASSERTIONS_ON_CONSTANTS, e.span, |
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.
The span_lint_and_sugg
should work. The problem you had was in the assert(true)
branch but you were still using span_lint
there.
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.
@mikerite I check it here:
https://github.com/rust-lang/rust-clippy/pull/3582/files/7075015f311617710dcebfe6530bdca732287994#diff-fc99a0c4e187ac372040420fe4dcb24cR60
Test case:
https://github.com/rust-lang/rust-clippy/pull/3582/files/7075015f311617710dcebfe6530bdca732287994#diff-514c75729f8046fc08cc8bb536b2fd0aR13
Failed CI on this test:
https://travis-ci.com/rust-lang/rust-clippy/jobs/169168148#L1259
Thanks for the update! I'll take a closer look at it ASAP. |
I didn't forget about this. I have a lot on my schedule right now. I'll try to get to this, this weekend. |
Sorry this took so long. This LGTM now. It's weird that the suggestion somehow didn't work, but this can be addressed in a later PR. @bors r+ Thanks! |
📌 Commit 58abdb5 has been approved by |
Add assert(true) and assert(false) lints This PR add two lints on assert!(true) and assert!(false). #3575
💔 Test failed - status-appveyor |
@bors rollup |
…lip1995 Add assert(true) and assert(false) lints This PR add two lints on assert!(true) and assert!(false). rust-lang#3575
…lip1995 Add assert(true) and assert(false) lints This PR add two lints on assert!(true) and assert!(false). rust-lang#3575
@flip1995 looks like I need to rebase it on fresh master, because of this: |
@Arkweid Adding |
* master: (58 commits) Rustfmt all the things Don't make decisions on values that don't represent the decision Improving comments. Rustup Added rustfix to the test. Improve span shortening. Added "make_return" and "blockify" convenience methods in Sugg and used them in "needless_bool". Actually check for constants. Fixed potential mistakes with nesting. Added tests. formatting fix Update clippy_lints/src/needless_bool.rs formatting fix Fixing typo in CONTRIBUTING.md Fix breakage due to rust-lang/rust#57651 needless bool lint suggestion is wrapped in brackets if it is an "else" clause of an "if-else" statement Fix automatic suggestion on `use_self`. Remove negative integer literal checks. Fix `implicit_return` false positives. Run rustfmt Fixed breakage due to rust-lang/rust#57489 ...
e86b6ce
to
c771f33
Compare
@flip1995 4 last commits squashed in one. |
@bors r+ Thanks! |
📌 Commit c771f33 has been approved by |
Add assert(true) and assert(false) lints This PR add two lints on assert!(true) and assert!(false). #3575
☀️ Test successful - checks-travis, status-appveyor |
This PR add two lints on assert!(true) and assert!(false).
#3575