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

New lint if_then_panic #7669

Merged
merged 1 commit into from
Sep 24, 2021
Merged

New lint if_then_panic #7669

merged 1 commit into from
Sep 24, 2021

Conversation

Labelray
Copy link
Contributor

@Labelray Labelray commented Sep 14, 2021

changelog: add the new lint [if_then_panic]
fix #7645

@rust-highfive
Copy link

r? @giraffate

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Sep 14, 2021
@Labelray Labelray force-pushed the if_then_panic branch 4 times, most recently from bf3194e to 5761bb7 Compare September 14, 2021 09:15
@camsteffen
Copy link
Contributor

camsteffen commented Sep 14, 2021

Use cx.sess().source_map().is_multiline(span) to make sure the condition fits on on line as noted in the issue. Add a negative test for this.

I think providing a code suggestion is important for this lint. The fix may not be intuitive and people may not be aware that assert! accepts a message and formatting args. We probably need PanicMacroExpn similar to FormatExpn.

@camsteffen
Copy link
Contributor

I'll leave it to @giraffate do to a full review. But note I still don't see the multi-line check. @Labelray don't hesitate to ask if something doesn't make sense!

@Labelray
Copy link
Contributor Author

I'll leave it to @giraffate do to a full review. But note I still don't see the multi-line check. @Labelray don't hesitate to ask if something doesn't make sense!

@camsteffen I didn't check the number of lines but I checked the number of statements. I think it is better.

@camsteffen
Copy link
Contributor

A long condition can be a single statement.

if some_function(foo, bar, baz) == another_function(hi, whats, up)
    && some_function(a, b, c) == another_function(a, b, c) {
    panic!();
}

clippy_lints/src/if_then_panic.rs Show resolved Hide resolved
clippy_lints/src/if_then_panic.rs Outdated Show resolved Hide resolved
@Labelray Labelray force-pushed the if_then_panic branch 3 times, most recently from 0d3c4f9 to 44ad749 Compare September 22, 2021 00:45
Copy link
Contributor

@giraffate giraffate left a comment

Choose a reason for hiding this comment

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

I made a comment but overall looks good, thanks!

Comment on lines 43 to 52
error: only a `panic!` in `if`-then statement
--> $DIR/fallible_impl_from.rs:28:9
|
LL | / if i != 42 {
LL | | panic!();
LL | | }
| |_________^ help: try: `assert!(!i != 42, "explicit panic");`
|
= note: `-D clippy::if-then-panic` implied by `-D warnings`

Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add #![allow(clippy::if_then_panic)] and remove these warnings in this test file? It looks a little confusing.

Copy link

@pravic pravic Oct 1, 2021

Choose a reason for hiding this comment

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

If this suggestion even valid?

!i != 42 is (!i) != 42 which is not what is intended to be suggested here. It should be !(i != 42).

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=a84683ba815f737b90acb285876d2365

Though, already reported: #7731

Copy link
Contributor

@giraffate giraffate left a comment

Choose a reason for hiding this comment

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

Can the conflict be resolved?

@giraffate
Copy link
Contributor

We follow a no merge-commit policy. Can you do a rebase, not a merge?

@giraffate
Copy link
Contributor

@bors r+

Thanks!

@bors
Copy link
Collaborator

bors commented Sep 24, 2021

📌 Commit 543b638 has been approved by giraffate

@bors
Copy link
Collaborator

bors commented Sep 24, 2021

⌛ Testing commit 543b638 with merge fb61d04...

@bors
Copy link
Collaborator

bors commented Sep 24, 2021

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: giraffate
Pushing fb61d04 to master...

@bors bors merged commit fb61d04 into rust-lang:master Sep 24, 2021
@Labelray Labelray deleted the if_then_panic branch September 25, 2021 03:08
sharnoff added a commit to neondatabase/neon that referenced this pull request Sep 30, 2021
Most of the changes are for the new if-then-panic lint added in
rust-lang/rust-clippy#7669.

Error messages look like:

  error: only a `panic!` in `if`-then statement
     --> zenith_utils/src/lsn.rs:195:9
      |
  195 | /         if prev.checked_add(val).is_none() {
  196 | |             panic!("AtomicLsn overflow");
  197 | |         }
      | |_________^ help: try: `assert!(!prev.checked_add(val).is_none(), "AtomicLsn overflow");`
      |
      = note: `-D clippy::if-then-panic` implied by `-D warnings`
      = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#if_then_panic
sharnoff added a commit to neondatabase/neon that referenced this pull request Oct 1, 2021
Most of the changes are for the new if-then-panic lint added in
rust-lang/rust-clippy#7669.
@Jasperav
Copy link

Jasperav commented Oct 19, 2021

@camsteffen @giraffate This issue causes regression: panic! can be catch with std::panic::catch_unwind, but assert! not. This broke my code and can break other people's code as well if panic! is unconditionally replaced with assert! (when using the catch_unwind since there won't be a catch but the whole program is terminated), so it should not be suggested as a 'fix'.

I would suggest reverting this lint or don't make it active by default (not sure if this is possible)

@Jasperav
Copy link

This lint also transformed this code:

let x = 10;
if x == 0 {
    panic!("fail");
}

to

let x = 10;
assert!(!x == 0, "fail");

which... is weird. Why negate an integer? This also breaks my code since this assertion is now triggered, while the initial code wasn't being triggered.

@camsteffen
Copy link
Contributor

Hi @Jasperav, thanks for the feedback.

This issue causes regression: panic! can be catch with std::panic::catch_unwind, but assert! not.

I don't think this is true, unless I misunderstood. See playground.

I would suggest reverting this lint or don't make it active by default

The lint is being moved to pedantic (off by default) in #7810.

which... is weird. Why negate an integer?

This should be fixed from #7741.

@Jasperav
Copy link

Jasperav commented Oct 20, 2021

Edit: apparently, it can indeed catch assertions. Not sure what went wrong with my code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

New lint: Manual assert
7 participants