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

manual_assert mangles code and introduces syntax errors #9886

Closed
matthiaskrgr opened this issue Nov 19, 2022 · 0 comments · Fixed by #10013
Closed

manual_assert mangles code and introduces syntax errors #9886

matthiaskrgr opened this issue Nov 19, 2022 · 0 comments · Fixed by #10013
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@matthiaskrgr
Copy link
Member

Summary

.

Lint Name

manual_assert

Reproducer

I tried this code:

#![deny(clippy::fallible_impl_from)]
#![allow(clippy::uninlined_format_args)]

// docs example
struct Foo(i32);
impl From<String> for Foo {
    fn from(s: String) -> Self {
        Foo(s.parse().unwrap())
    }
}

struct Invalid;

impl From<usize> for Invalid {
    fn from(i: usize) -> Invalid {
        if i != 42 {
            panic!();
        }
        Invalid
    }
}

impl From<Option<String>> for Invalid {
    fn from(s: Option<String>) -> Invalid {
        let s = s.unwrap();
        if !s.is_empty() {
            panic!("42");
        } else if s.parse::<u32>().unwrap() != 42 {
            panic!("{:?}", s);
        }
        Invalid
    }
}

fn main() {}

I saw this happen:
Looks like the suggestion eats the curly braces from the else block :/

cargo clippy --fix -- -Wclippy::manual_assert

   Checking a v0.1.0 (/tmp/a)
warning: failed to automatically apply fixes suggested by rustc to crate `a`

after fixes were automatically applied the compiler reported errors within these files:

  * src/main.rs

This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag

The following errors were reported:
error: expected `{`, found `assert`
  --> src/main.rs:28:16
   |
28 |         } else assert!(!(s.parse::<u32>().unwrap() != 42), "{:?}", s);
   |                ^^^^^^ expected `{`
   |
help: try placing this code inside a block
   |
28 |         } else { assert!(!(s.parse::<u32>().unwrap() != 42), "{:?}", s) };
   |                +                                                        +

error: aborting due to previous error

Original diagnostics will follow.

warning: consider implementing `TryFrom` instead
  --> src/main.rs:6:1
   |
6  | / impl From<String> for Foo {
7  | |     fn from(s: String) -> Self {
8  | |         Foo(s.parse().unwrap())
9  | |     }
10 | | }
   | |_^
   |
   = help: `From` is intended for infallible conversions only. Use `TryFrom` if there's a possibility for the conversion to fail
note: potential failure(s)
  --> src/main.rs:8:13
   |
8  |         Foo(s.parse().unwrap())
   |             ^^^^^^^^^^^^^^^^^^
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#fallible_impl_from
note: the lint level is defined here
  --> src/main.rs:1:9
   |
1  | #![deny(clippy::fallible_impl_from)]
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^

warning: consider implementing `TryFrom` instead
  --> src/main.rs:14:1
   |
14 | / impl From<usize> for Invalid {
15 | |     fn from(i: usize) -> Invalid {
16 | |         if i != 42 {
17 | |             panic!();
...  |
20 | |     }
21 | | }
   | |_^
   |
   = help: `From` is intended for infallible conversions only. Use `TryFrom` if there's a possibility for the conversion to fail
note: potential failure(s)
  --> src/main.rs:17:13
   |
17 |             panic!();
   |             ^^^^^^^^
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#fallible_impl_from
   = note: this warning originates in the macro `$crate::panic::panic_2021` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info)

warning: consider implementing `TryFrom` instead
  --> src/main.rs:23:1
   |
23 | / impl From<Option<String>> for Invalid {
24 | |     fn from(s: Option<String>) -> Invalid {
25 | |         let s = s.unwrap();
26 | |         if !s.is_empty() {
...  |
32 | |     }
33 | | }
   | |_^
   |
   = help: `From` is intended for infallible conversions only. Use `TryFrom` if there's a possibility for the conversion to fail
note: potential failure(s)
  --> src/main.rs:25:17
   |
25 |         let s = s.unwrap();
   |                 ^^^^^^^^^^
26 |         if !s.is_empty() {
27 |             panic!("42");
   |             ^^^^^^^^^^^^
28 |         } else if s.parse::<u32>().unwrap() != 42 {
   |                   ^^^^^^^^^^^^^^^^^^^^^^^^^
29 |             panic!("{:?}", s);
   |             ^^^^^^^^^^^^^^^^^
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#fallible_impl_from
   = note: this warning originates in the macro `$crate::panic::panic_2021` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info)

warning: only a `panic!` in `if`-then statement
  --> src/main.rs:28:16
   |
28 |           } else if s.parse::<u32>().unwrap() != 42 {
   |  ________________^
29 | |             panic!("{:?}", s);
30 | |         }
   | |_________^ help: try instead: `assert!(!(s.parse::<u32>().unwrap() != 42), "{:?}", s);`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_assert
   = note: requested on the command line with `-W clippy::manual-assert`

Version

rustc 1.67.0-nightly (b833ad56f 2022-11-18)
binary: rustc
commit-hash: b833ad56f46a0bbe0e8729512812a161e7dae28a
commit-date: 2022-11-18
host: x86_64-unknown-linux-gnu
release: 1.67.0-nightly
LLVM version: 15.0.4

Additional Labels

No response

@matthiaskrgr matthiaskrgr added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied labels Nov 19, 2022
@bors bors closed this as completed in fec0057 Dec 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant