-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Add lint for use of ^ operator as exponentiation. #4541
Closed
Closed
Changes from all commits
Commits
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 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
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,77 @@ | ||
use crate::utils::{span_help_and_lint, span_lint_and_sugg}; | ||
use if_chain::if_chain; | ||
use rustc::lint::{in_external_macro, EarlyContext, EarlyLintPass, LintArray, LintPass}; | ||
use rustc::{declare_lint_pass, declare_tool_lint}; | ||
use rustc_errors::Applicability; | ||
use syntax::ast::{BinOpKind, Expr, ExprKind, LitKind}; | ||
|
||
declare_clippy_lint! { | ||
/// **What it does:** Checks for use of `^` operator when exponentiation was intended. | ||
/// | ||
/// **Why is this bad?** This is most probably a typo. | ||
/// | ||
/// **Known problems:** None. | ||
/// | ||
/// **Example:** | ||
/// | ||
/// ```rust,ignore | ||
/// // Bad | ||
/// 2 ^ 16; | ||
/// | ||
/// // Good | ||
/// 1 << 16; | ||
/// 2i32.pow(16); | ||
/// ``` | ||
pub XOR_USED_AS_POW, | ||
correctness, | ||
"use of `^` operator when exponentiation was intended" | ||
} | ||
|
||
declare_lint_pass!(XorUsedAsPow => [XOR_USED_AS_POW]); | ||
|
||
impl EarlyLintPass for XorUsedAsPow { | ||
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) { | ||
if_chain! { | ||
if !in_external_macro(cx.sess, expr.span); | ||
if let ExprKind::Binary(op, left, right) = &expr.node; | ||
if BinOpKind::BitXor == op.node; | ||
if let ExprKind::Lit(lit) = &left.node; | ||
if let LitKind::Int(lhs, _) = lit.node; | ||
if let ExprKind::Lit(lit) = &right.node; | ||
if let LitKind::Int(rhs, _) = lit.node; | ||
then { | ||
if lhs == 2 { | ||
if rhs == 8 || rhs == 16 || rhs == 32 || rhs == 64 { | ||
span_lint_and_sugg( | ||
cx, | ||
XOR_USED_AS_POW, | ||
expr.span, | ||
"it appears you are trying to get the maximum value of an integer, but `^` is not an exponentiation operator", | ||
"try", | ||
format!("std::u{}::MAX", rhs), | ||
Applicability::MaybeIncorrect, | ||
) | ||
} else { | ||
span_lint_and_sugg( | ||
cx, | ||
XOR_USED_AS_POW, | ||
expr.span, | ||
"it appears you are trying to get a power of two, but `^` is not an exponentiation operator", | ||
"use a bitshift instead", | ||
format!("1 << {}", rhs), | ||
Applicability::MaybeIncorrect, | ||
) | ||
} | ||
} else { | ||
span_help_and_lint( | ||
cx, | ||
XOR_USED_AS_POW, | ||
expr.span, | ||
"`^` is not an exponentiation operator but appears to have been used as one", | ||
"did you mean to use .pow()?" | ||
) | ||
} | ||
} | ||
} | ||
} | ||
} |
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,15 @@ | ||
#![warn(clippy::xor_used_as_pow)] | ||
|
||
fn main() { | ||
// These should succeed | ||
// With variables, it's not as clear whether the intention was exponentiation or not | ||
let x = 15; | ||
println!("{}", 2 ^ x); | ||
let y = 2; | ||
println!("{}", y ^ 16); | ||
|
||
// These should fail | ||
println!("{}", 2 ^ 16); | ||
println!("{}", 2 ^ 7); | ||
println!("{}", 9 ^ 3); | ||
} |
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,24 @@ | ||
error: it appears you are trying to get the maximum value of an integer, but `^` is not an exponentiation operator | ||
--> $DIR/xor_used_as_pow.rs:12:20 | ||
| | ||
LL | println!("{}", 2 ^ 16); | ||
| ^^^^^^ help: try: `std::u16::MAX` | ||
| | ||
= note: `-D clippy::xor-used-as-pow` implied by `-D warnings` | ||
|
||
error: it appears you are trying to get a power of two, but `^` is not an exponentiation operator | ||
--> $DIR/xor_used_as_pow.rs:13:20 | ||
| | ||
LL | println!("{}", 2 ^ 7); | ||
| ^^^^^ help: use a bitshift instead: `1 << 7` | ||
|
||
error: `^` is not an exponentiation operator but appears to have been used as one | ||
--> $DIR/xor_used_as_pow.rs:14:20 | ||
| | ||
LL | println!("{}", 9 ^ 3); | ||
| ^^^^^ | ||
| | ||
= help: did you mean to use .pow()? | ||
|
||
error: aborting due to 3 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we account for u128 as well?