Skip to content

New lint if_then_panic - #7669

Merged
bors merged 1 commit into
rust-lang:masterfrom
Labelray:if_then_panic
Sep 24, 2021
Merged

bors merged 1 commit into
rust-lang:masterfrom
Labelray:if_then_panic

Conversation

@Labelray

@Labelray Labelray commented Sep 14, 2021 •

Copy link
Copy Markdown
Contributor

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

@rust-highfive

Copy link
Copy Markdown

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

camsteffen commented Sep 14, 2021 •

Copy link
Copy Markdown
Contributor

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.

Comment thread clippy_utils/src/higher.rs Outdated
@camsteffen

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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!();
}

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

@giraffate giraffate left a comment

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.

I made a comment but overall looks good, thanks!

Comment thread tests/ui/fallible_impl_from.stderr Outdated
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
Copy Markdown
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.

@pravic pravic Oct 1, 2021 •

Copy link
Copy Markdown

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

@giraffate giraffate left a comment

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.

Can the conflict be resolved?

@giraffate

Copy link
Copy Markdown
Contributor

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

@giraffate

Copy link
Copy Markdown
Contributor

@bors r+

Thanks!

@bors

bors commented Sep 24, 2021

Copy link
Copy Markdown
Contributor

📌 Commit 543b638 has been approved by giraffate

@bors

bors commented Sep 24, 2021

Copy link
Copy Markdown
Contributor

⌛ Testing commit 543b638 with merge fb61d04...

@bors

bors commented Sep 24, 2021

Copy link
Copy Markdown
Contributor

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

Jasperav commented Oct 19, 2021 •

Copy link
Copy Markdown

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

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

Jasperav commented Oct 20, 2021 •

Copy link
Copy Markdown

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

zeekay pushed a commit to hanzosql/neon that referenced this pull request Sep 10, 2026
Most of the changes are for the new if-then-panic lint added in
rust-lang/rust-clippy#7669.
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