Skip to content

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

Merged
bors merged 9 commits into
rust-lang:masterfrom
Arkweid:add-lints-aseert-checks
Jan 23, 2019
Merged

Add assert(true) and assert(false) lints#3582
bors merged 9 commits into
rust-lang:masterfrom
Arkweid:add-lints-aseert-checks

Conversation

@Arkweid

@Arkweid Arkweid commented Dec 25, 2018

Copy link
Copy Markdown

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

@matthiaskrgr matthiaskrgr left a comment

Copy link
Copy Markdown
Member

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.

Comment thread clippy_lints/src/assert_checks.rs Outdated
@Arkweid

Arkweid commented Dec 27, 2018

Copy link
Copy Markdown
Author

Error message changed to:

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

@ghost

ghost commented Dec 27, 2018

Copy link
Copy Markdown

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

@flip1995 flip1995 left a comment

Copy link
Copy Markdown
Member

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")

Comment thread clippy_lints/src/assert_checks.rs Outdated
Comment thread clippy_lints/src/assert_checks.rs Outdated
Comment thread clippy_lints/src/assert_checks.rs Outdated
Comment thread clippy_lints/src/assert_checks.rs Outdated
then {
match inner.node {
LitKind::Bool(true) => {
span_lint(cx, EXPLICIT_TRUE, e.span,

Copy link
Copy Markdown
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

Arkweid commented Dec 27, 2018

Copy link
Copy Markdown
Author

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

@bors

bors commented Dec 29, 2018

Copy link
Copy Markdown
Contributor

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

@flip1995

flip1995 commented Jan 4, 2019

Copy link
Copy Markdown
Member

ping from triage @Arkweid: Any updates on this?

@Arkweid

Arkweid commented Jan 4, 2019

Copy link
Copy Markdown
Author

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

@Arkweid Arkweid force-pushed the add-lints-aseert-checks branch from 6a100eb to 906b516 Compare January 9, 2019 10:40
Comment thread 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.

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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

Arkweid commented Jan 9, 2019

Copy link
Copy Markdown
Author

@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
match inner.node {
LitKind::Bool(true) => {
span_lint(cx, EXPLICIT_TRUE, e.span,
span_lint(cx, ASSERTIONS_ON_CONSTANTS, e.span,

Copy link
Copy Markdown

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
Copy Markdown
Author

Choose a reason for hiding this comment

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

@flip1995

Copy link
Copy Markdown
Member

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

@flip1995

Copy link
Copy Markdown
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
Copy Markdown
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

bors commented Jan 22, 2019

Copy link
Copy Markdown
Contributor

📌 Commit 58abdb5 has been approved by flip1995

@bors

bors commented Jan 22, 2019

Copy link
Copy Markdown
Contributor

⌛ 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

bors commented Jan 22, 2019

Copy link
Copy Markdown
Contributor

💔 Test failed - status-appveyor

@flip1995

Copy link
Copy Markdown
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

Arkweid commented Jan 22, 2019

Copy link
Copy Markdown
Author

@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

phansch commented Jan 23, 2019

Copy link
Copy Markdown
Contributor

@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

Arkweid commented Jan 23, 2019

Copy link
Copy Markdown
Author

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

@flip1995

Copy link
Copy Markdown
Member

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

@Arkweid Arkweid force-pushed the add-lints-aseert-checks branch from e86b6ce to c771f33 Compare January 23, 2019 15:20
@Arkweid

Arkweid commented Jan 23, 2019

Copy link
Copy Markdown
Author

@flip1995 4 last commits squashed in one.

@flip1995

Copy link
Copy Markdown
Member

@bors r+ Thanks!

@bors

bors commented Jan 23, 2019

Copy link
Copy Markdown
Contributor

📌 Commit c771f33 has been approved by flip1995

@bors

bors commented Jan 23, 2019

Copy link
Copy Markdown
Contributor

⌛ 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

bors commented Jan 23, 2019

Copy link
Copy Markdown
Contributor

☀️ 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