-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
192 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use clippy_utils::diagnostics::span_lint_and_help; | ||
use clippy_utils::{path_to_local, path_to_local_id}; | ||
use rustc_hir::{Expr, ExprKind, Node}; | ||
use rustc_lint::LateContext; | ||
|
||
use super::EAGER_INT_TRANSMUTE; | ||
|
||
fn peel_parent_unsafe_blocks<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) -> Option<&'tcx Expr<'tcx>> { | ||
for (_, parent) in cx.tcx.hir().parent_iter(expr.hir_id) { | ||
match parent { | ||
Node::Block(_) => {}, | ||
Node::Expr(e) if let ExprKind::Block(..) = e.kind => {}, | ||
Node::Expr(e) => return Some(e), | ||
_ => break, | ||
} | ||
} | ||
None | ||
} | ||
|
||
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>, transmutable: &'tcx Expr<'tcx>) -> bool { | ||
if let Some(then_some_call) = peel_parent_unsafe_blocks(cx, expr) | ||
&& let ExprKind::MethodCall(path, receiver, ..) = then_some_call.kind | ||
&& cx.typeck_results().expr_ty(receiver).is_bool() | ||
&& path.ident.name == sym!(then_some) | ||
&& let ExprKind::Binary(_, lhs, _) = receiver.kind | ||
&& let Some(local_id) = path_to_local(transmutable) | ||
&& path_to_local_id(lhs, local_id) | ||
{ | ||
span_lint_and_help( | ||
cx, | ||
EAGER_INT_TRANSMUTE, | ||
expr.span, | ||
"this transmute is evaluated eagerly, even if the condition is false", | ||
Some(path.ident.span), | ||
"consider using `then` instead, which evaluates it only if the condition is true", | ||
); | ||
} | ||
false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#![warn(clippy::eager_int_transmute)] | ||
|
||
#[repr(u8)] | ||
enum Opcode { | ||
Add = 0, | ||
Sub = 1, | ||
Mul = 2, | ||
Div = 3, | ||
} | ||
|
||
fn int_to_opcode(op: u8) -> Option<Opcode> { | ||
(op < 4).then_some(unsafe { std::mem::transmute(op) }) | ||
} | ||
|
||
fn f(op: u8, unrelated: u8) { | ||
true.then_some(unsafe { std::mem::transmute::<_, Opcode>(op) }); | ||
(unrelated < 4).then_some(unsafe { std::mem::transmute::<_, Opcode>(op) }); | ||
(op < 4).then_some(unsafe { std::mem::transmute::<_, Opcode>(op) }); | ||
(op > 4).then_some(unsafe { std::mem::transmute::<_, Opcode>(op) }); | ||
(op == 0).then_some(unsafe { std::mem::transmute::<_, Opcode>(op) }); | ||
} | ||
|
||
unsafe fn f2(op: u8) { | ||
(op < 4).then_some(std::mem::transmute::<_, Opcode>(op)); | ||
} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
error: this transmute is evaluated eagerly, even if the condition is false | ||
--> $DIR/eager_int_transmute.rs:12:33 | ||
| | ||
LL | (op < 4).then_some(unsafe { std::mem::transmute(op) }) | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
help: consider using `then` instead, which evaluates it only if the condition is true | ||
--> $DIR/eager_int_transmute.rs:12:14 | ||
| | ||
LL | (op < 4).then_some(unsafe { std::mem::transmute(op) }) | ||
| ^^^^^^^^^ | ||
= note: `-D clippy::eager-int-transmute` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::eager_int_transmute)]` | ||
|
||
error: this transmute is evaluated eagerly, even if the condition is false | ||
--> $DIR/eager_int_transmute.rs:18:33 | ||
| | ||
LL | (op < 4).then_some(unsafe { std::mem::transmute::<_, Opcode>(op) }); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
help: consider using `then` instead, which evaluates it only if the condition is true | ||
--> $DIR/eager_int_transmute.rs:18:14 | ||
| | ||
LL | (op < 4).then_some(unsafe { std::mem::transmute::<_, Opcode>(op) }); | ||
| ^^^^^^^^^ | ||
|
||
error: this transmute is evaluated eagerly, even if the condition is false | ||
--> $DIR/eager_int_transmute.rs:19:33 | ||
| | ||
LL | (op > 4).then_some(unsafe { std::mem::transmute::<_, Opcode>(op) }); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
help: consider using `then` instead, which evaluates it only if the condition is true | ||
--> $DIR/eager_int_transmute.rs:19:14 | ||
| | ||
LL | (op > 4).then_some(unsafe { std::mem::transmute::<_, Opcode>(op) }); | ||
| ^^^^^^^^^ | ||
|
||
error: this transmute is evaluated eagerly, even if the condition is false | ||
--> $DIR/eager_int_transmute.rs:20:34 | ||
| | ||
LL | (op == 0).then_some(unsafe { std::mem::transmute::<_, Opcode>(op) }); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
help: consider using `then` instead, which evaluates it only if the condition is true | ||
--> $DIR/eager_int_transmute.rs:20:15 | ||
| | ||
LL | (op == 0).then_some(unsafe { std::mem::transmute::<_, Opcode>(op) }); | ||
| ^^^^^^^^^ | ||
|
||
error: this transmute is evaluated eagerly, even if the condition is false | ||
--> $DIR/eager_int_transmute.rs:24:24 | ||
| | ||
LL | (op < 4).then_some(std::mem::transmute::<_, Opcode>(op)); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
help: consider using `then` instead, which evaluates it only if the condition is true | ||
--> $DIR/eager_int_transmute.rs:24:14 | ||
| | ||
LL | (op < 4).then_some(std::mem::transmute::<_, Opcode>(op)); | ||
| ^^^^^^^^^ | ||
|
||
error: aborting due to 5 previous errors | ||
|