-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add lint for confusing use of ^
instead of .pow
#9506
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,53 @@ | ||
use clippy_utils::{numeric_literal::NumericLiteral, source::snippet_with_context}; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::{BinOpKind, Expr, ExprKind}; | ||
use rustc_lint::{LateContext, LateLintPass, LintContext}; | ||
use rustc_middle::lint::in_external_macro; | ||
use rustc_session::{declare_lint_pass, declare_tool_lint}; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// Warns for a Bitwise XOR (`^`) operator being probably confused as a powering. It will not trigger if any of the numbers are not in decimal. | ||
/// ### Why is this bad? | ||
/// It's most probably a typo and may lead to unexpected behaviours. | ||
/// ### Example | ||
/// ```rust | ||
/// let x = 3_i32 ^ 4_i32; | ||
/// ``` | ||
/// Use instead: | ||
/// ```rust | ||
/// let x = 3_i32.pow(4); | ||
/// ``` | ||
#[clippy::version = "1.66.0"] | ||
pub SUSPICIOUS_XOR_USED_AS_POW, | ||
restriction, | ||
"XOR (`^`) operator possibly used as exponentiation operator" | ||
} | ||
declare_lint_pass!(ConfusingXorAndPow => [SUSPICIOUS_XOR_USED_AS_POW]); | ||
|
||
impl LateLintPass<'_> for ConfusingXorAndPow { | ||
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) { | ||
if !in_external_macro(cx.sess(), expr.span) && | ||
let ExprKind::Binary(op, left, right) = &expr.kind && | ||
op.node == BinOpKind::BitXor && | ||
left.span.ctxt() == right.span.ctxt() && | ||
let ExprKind::Lit(lit_left) = &left.kind && | ||
let ExprKind::Lit(lit_right) = &right.kind && | ||
let snip_left = snippet_with_context(cx, lit_left.span, lit_left.span.ctxt(), "..", &mut Applicability::MaybeIncorrect) && | ||
let snip_right = snippet_with_context(cx, lit_right.span, lit_right.span.ctxt(), "..", &mut Applicability::MaybeIncorrect) && | ||
let Some(left_val) = NumericLiteral::from_lit_kind(&snip_left.0, &lit_left.node) && | ||
let Some(right_val) = NumericLiteral::from_lit_kind(&snip_right.0, &lit_right.node) && | ||
left_val.is_decimal() && | ||
right_val.is_decimal() { | ||
clippy_utils::diagnostics::span_lint_and_sugg( | ||
cx, | ||
SUSPICIOUS_XOR_USED_AS_POW, | ||
expr.span, | ||
"`^` is not the exponentiation operator", | ||
"did you mean to write", | ||
format!("{}.pow({})", left_val.format(), right_val.format()), | ||
Applicability::MaybeIncorrect, | ||
); | ||
} | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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,34 @@ | ||
#![allow(unused)] | ||
#![warn(clippy::suspicious_xor_used_as_pow)] | ||
#![allow(clippy::eq_op)] | ||
|
||
macro_rules! macro_test { | ||
() => { | ||
13 | ||
}; | ||
} | ||
|
||
macro_rules! macro_test_inside { | ||
() => { | ||
1 ^ 2 // should warn even if inside macro | ||
}; | ||
} | ||
|
||
fn main() { | ||
// Should warn: | ||
let _ = 2 ^ 5; | ||
let _ = 2i32 ^ 9i32; | ||
let _ = 2i32 ^ 2i32; | ||
let _ = 50i32 ^ 3i32; | ||
let _ = 5i32 ^ 8i32; | ||
let _ = 2i32 ^ 32i32; | ||
macro_test_inside!(); | ||
|
||
// Should not warn: | ||
let x = 0x02; | ||
let _ = x ^ 2; | ||
let _ = 2 ^ x; | ||
let _ = x ^ 5; | ||
let _ = 10 ^ 0b0101; | ||
let _ = 2i32 ^ macro_test!(); | ||
} |
This file contains hidden or 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,51 @@ | ||
error: `^` is not the exponentiation operator | ||
--> $DIR/suspicious_xor_used_as_pow.rs:19:13 | ||
| | ||
LL | let _ = 2 ^ 5; | ||
| ^^^^^ help: did you mean to write: `2.pow(5)` | ||
| | ||
= note: `-D clippy::suspicious-xor-used-as-pow` implied by `-D warnings` | ||
|
||
error: `^` is not the exponentiation operator | ||
--> $DIR/suspicious_xor_used_as_pow.rs:20:13 | ||
| | ||
LL | let _ = 2i32 ^ 9i32; | ||
| ^^^^^^^^^^^ help: did you mean to write: `2_i32.pow(9_i32)` | ||
|
||
error: `^` is not the exponentiation operator | ||
--> $DIR/suspicious_xor_used_as_pow.rs:21:13 | ||
| | ||
LL | let _ = 2i32 ^ 2i32; | ||
| ^^^^^^^^^^^ help: did you mean to write: `2_i32.pow(2_i32)` | ||
|
||
error: `^` is not the exponentiation operator | ||
--> $DIR/suspicious_xor_used_as_pow.rs:22:13 | ||
| | ||
LL | let _ = 50i32 ^ 3i32; | ||
| ^^^^^^^^^^^^ help: did you mean to write: `50_i32.pow(3_i32)` | ||
|
||
error: `^` is not the exponentiation operator | ||
--> $DIR/suspicious_xor_used_as_pow.rs:23:13 | ||
| | ||
LL | let _ = 5i32 ^ 8i32; | ||
| ^^^^^^^^^^^ help: did you mean to write: `5_i32.pow(8_i32)` | ||
|
||
error: `^` is not the exponentiation operator | ||
--> $DIR/suspicious_xor_used_as_pow.rs:24:13 | ||
| | ||
LL | let _ = 2i32 ^ 32i32; | ||
| ^^^^^^^^^^^^ help: did you mean to write: `2_i32.pow(32_i32)` | ||
|
||
error: `^` is not the exponentiation operator | ||
--> $DIR/suspicious_xor_used_as_pow.rs:13:9 | ||
| | ||
LL | 1 ^ 2 // should warn even if inside macro | ||
| ^^^^^ help: did you mean to write: `1.pow(2)` | ||
... | ||
LL | macro_test_inside!(); | ||
| -------------------- in this macro invocation | ||
| | ||
= note: this error originates in the macro `macro_test_inside` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
||
error: aborting due to 7 previous errors | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.