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

Add assert(true) and assert(false) lints #3582

Merged
merged 9 commits into from
Jan 23, 2019

Conversation

Arkweid
Copy link

@Arkweid Arkweid commented Dec 25, 2018

This PR add two lints on assert!(true) and assert!(false).
#3575

@flip1995 flip1995 added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Dec 26, 2018
Copy link
Member

@matthiaskrgr matthiaskrgr left a 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.

clippy_lints/src/assert_checks.rs Outdated Show resolved Hide resolved
@Arkweid
Copy link
Author

Arkweid commented Dec 27, 2018

Error message changed to:

assert!(false) should probably be replaced by a panic!() or unreachable!()

@ghost
Copy link

ghost commented Dec 27, 2018

I would combine these lints into one. Maybe call it assertions_on_constants... 🤔

Copy link
Member

@flip1995 flip1995 left a 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, suggest panic!("_") instead of just panic!() (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 Show resolved Hide resolved
clippy_lints/src/assert_checks.rs Outdated Show resolved Hide resolved
clippy_lints/src/assert_checks.rs Outdated Show resolved Hide resolved
then {
match inner.node {
LitKind::Bool(true) => {
span_lint(cx, EXPLICIT_TRUE, e.span,
Copy link
Member

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

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.

@flip1995 flip1995 added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Dec 27, 2018
@Arkweid
Copy link
Author

Arkweid commented Dec 27, 2018

Thanks for your review @flip1995 👍
I will make changes in this PR soon.

@bors
Copy link
Collaborator

bors commented Dec 29, 2018

☔ The latest upstream changes (presumably #3558) made this pull request unmergeable. Please resolve the merge conflicts.

@flip1995
Copy link
Member

flip1995 commented Jan 4, 2019

ping from triage @Arkweid: Any updates on this?

@Arkweid
Copy link
Author

Arkweid commented Jan 4, 2019

@flip1995 Ye, I am here. Just holidays in Russia until 9 January :)

// <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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// run-rustfix

e.span,
"assert!(false) should probably be replaced",
"try",
"panic!()".to_string(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

panic!() or unreachable!()

Copy link
Author

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?

@Arkweid
Copy link
Author

Arkweid commented Jan 9, 2019

@flip1995 changes pushed
Also, I faced with an issue where suggestion from span_lint_and_sugg was suppressed somewhere in rustc internals(probably). I have a conversation with @oli-obk about it in discord clippy channel. So I replace it with span_help_and_lint function. Because of this problem, I skip that feature assert!(false, "_") call, suggest panic!("_") instead of just panic!() (advanced)

@phansch phansch added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties and removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels Jan 9, 2019
@@ -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,
Copy link

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.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@flip1995
Copy link
Member

Thanks for the update! I'll take a closer look at it ASAP.

@flip1995
Copy link
Member

I didn't forget about this. I have a lot on my schedule right now. I'll try to get to this, this weekend.

@flip1995
Copy link
Member

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!

@bors
Copy link
Collaborator

bors commented Jan 22, 2019

📌 Commit 58abdb5 has been approved by flip1995

@bors
Copy link
Collaborator

bors commented Jan 22, 2019

⌛ Testing commit 58abdb5 with merge 9cee601...

bors added a commit that referenced this pull request Jan 22, 2019
Add assert(true) and assert(false) lints

This PR add two lints on assert!(true) and assert!(false).
#3575
@bors
Copy link
Collaborator

bors commented Jan 22, 2019

💔 Test failed - status-appveyor

@flip1995
Copy link
Member

@bors rollup

flip1995 added a commit to flip1995/rust-clippy that referenced this pull request Jan 22, 2019
…lip1995

Add assert(true) and assert(false) lints

This PR add two lints on assert!(true) and assert!(false).
rust-lang#3575
flip1995 added a commit to flip1995/rust-clippy that referenced this pull request Jan 22, 2019
…lip1995

Add assert(true) and assert(false) lints

This PR add two lints on assert!(true) and assert!(false).
rust-lang#3575
bors added a commit that referenced this pull request Jan 22, 2019
Rollup of 4 pull requests

Successful merges:

 - #3582 (Add assert(true) and assert(false) lints)
 - #3679 (Fix automatic suggestion on `use_self`.)
 - #3684 ("make_return" and "blockify" convenience methods, fixes #3683)
 - #3685 (Rustup)

Failed merges:

r? @ghost
@Arkweid
Copy link
Author

Arkweid commented Jan 22, 2019

@flip1995 looks like I need to rebase it on fresh master, because of this:
https://github.com/rust-lang/rust-clippy/blob/master/tests/ui/collapsible_if.rs#L143
Or just to add #![allow(clippy::assertions_on_constants::assertions_on_constants)] in collapsible_if.rs
Previously it not trigger an error :(

@flip1995 flip1995 added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Jan 22, 2019
@phansch
Copy link
Member

phansch commented Jan 23, 2019

@Arkweid Adding #![allow(clippy::assertions_on_constants)] to that file should be enough 👍

* 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
  ...
@Arkweid
Copy link
Author

Arkweid commented Jan 23, 2019

@flip1995 @phansch
Compatibility work with current master completed :D

@flip1995
Copy link
Member

Nice! Could you squash the last few commits 2068463..e86b6ce?

@Arkweid
Copy link
Author

Arkweid commented Jan 23, 2019

@flip1995 4 last commits squashed in one.

@flip1995
Copy link
Member

@bors r+ Thanks!

@bors
Copy link
Collaborator

bors commented Jan 23, 2019

📌 Commit c771f33 has been approved by flip1995

@bors
Copy link
Collaborator

bors commented Jan 23, 2019

⌛ Testing commit c771f33 with merge 99c5388...

bors added a commit that referenced this pull request Jan 23, 2019
Add assert(true) and assert(false) lints

This PR add two lints on assert!(true) and assert!(false).
#3575
@bors
Copy link
Collaborator

bors commented Jan 23, 2019

☀️ Test successful - checks-travis, status-appveyor
Approved by: flip1995
Pushing 99c5388 to master...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants