Skip to content

Incorrect macro handling in bool_to_int_with_if #14628

@samueltardieu

Description

@samueltardieu

Summary

.

Reproducer

I tried this code:

#![allow(unused)]

fn main() {
    macro_rules! mac {
        (one) => { 1 };
    }

    let _ = if dbg!(4 > 0) { 1 } else { 0 };
    let _ = if 4 > 0 { mac!(one) } else { 0 };
    let _ = if 4 > 0 {
        // This is an important comment
        1
    } else {
        0
    };
}

I expected to see this happen:

8 |     let _ = if dbg!(4 > 0) { 1 } else { 0 };
  |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with from: `i32::from(dbg!(4 > 0))`
= note: `dbg!(4 > 0) as i32` or `dbg!(4 > 0).into()` can also be valid options
10 |       let _ = if 4 > 0 {
   |  _____________^
11 | |         // This is an important comment
12 | |         1
13 | |     } else {
14 | |         0
15 | |     };
   | |_____^ help: replace with from: `i32::from(4 > 0)`
= note: `(4 > 0) as i32` or `(4 > 0).into()` can also be valid options

with the second suggestion being MaybeIncorrect because of the potential comments loss.

Instead, this happened:

error: boolean to int conversion using if
 --> t.rs:8:13
  |
8 |     let _ = if dbg!(4 > 0) { 1 } else { 0 };
  |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: `match $val {
                      tmp => {
                          $crate::eprintln!("[{}:{}:{}] {} = {:#?}",
                              $crate::file!(), $crate::line!(), $crate::column!(), $crate::stringify!($val), &tmp);
                          tmp
                      }
                  } as i32` or `(match $val {
                      tmp => {
                          $crate::eprintln!("[{}:{}:{}] {} = {:#?}",
                              $crate::file!(), $crate::line!(), $crate::column!(), $crate::stringify!($val), &tmp);
                          tmp
                      }
                  }).into()` can also be valid options
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#bool_to_int_with_if
note: the lint level is defined here
 --> t.rs:1:9
  |
1 | #![deny(clippy::bool_to_int_with_if)]
  |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: replace with from
  |
8 ~     let _ = i32::from(match $val {
9 +             tmp => {
10+                 $crate::eprintln!("[{}:{}:{}] {} = {:#?}",
11+                     $crate::file!(), $crate::line!(), $crate::column!(), $crate::stringify!($val), &tmp);
12+                 tmp
13+             }
14~         });
  |

error: boolean to int conversion using if
 --> t.rs:9:13
  |
9 |     let _ = if 4 > 0 { mac!(one) } else { 0 };
  |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with from: `i32::from(4 > 0)`
  |
  = note: `(4 > 0) as i32` or `(4 > 0).into()` can also be valid options
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#bool_to_int_with_if

error: boolean to int conversion using if
  --> t.rs:10:13
   |
10 |       let _ = if 4 > 0 {
   |  _____________^
11 | |         // This is an important comment
12 | |         1
13 | |     } else {
14 | |         0
15 | |     };
   | |_____^ help: replace with from: `i32::from(4 > 0)`
   |
   = note: `(4 > 0) as i32` or `(4 > 0).into()` can also be valid options
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#bool_to_int_with_if

error: aborting due to 3 previous errors

The first two suggestions are incorrect, the third one is MachineApplicable despite the loss of comments.

Version


Additional Labels

@rustbot claim

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions