-
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 new lint manual_is_power_of_two
#13327
Merged
Merged
Changes from all commits
Commits
Show all changes
4 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 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,142 @@ | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::source::snippet_with_applicability; | ||
use clippy_utils::SpanlessEq; | ||
use rustc_ast::LitKind; | ||
use rustc_data_structures::packed::Pu128; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::{BinOpKind, Expr, ExprKind}; | ||
use rustc_lint::{LateContext, LateLintPass}; | ||
use rustc_middle::ty::Uint; | ||
use rustc_session::declare_lint_pass; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// Checks for expressions like `x.count_ones() == 1` or `x & (x - 1) == 0`, with x and unsigned integer, which are manual | ||
/// reimplementations of `x.is_power_of_two()`. | ||
/// ### Why is this bad? | ||
/// Manual reimplementations of `is_power_of_two` increase code complexity for little benefit. | ||
/// ### Example | ||
/// ```no_run | ||
/// let a: u32 = 4; | ||
/// let result = a.count_ones() == 1; | ||
/// ``` | ||
/// Use instead: | ||
/// ```no_run | ||
/// let a: u32 = 4; | ||
/// let result = a.is_power_of_two(); | ||
/// ``` | ||
#[clippy::version = "1.82.0"] | ||
pub MANUAL_IS_POWER_OF_TWO, | ||
complexity, | ||
"manually reimplementing `is_power_of_two`" | ||
} | ||
|
||
declare_lint_pass!(ManualIsPowerOfTwo => [MANUAL_IS_POWER_OF_TWO]); | ||
|
||
impl LateLintPass<'_> for ManualIsPowerOfTwo { | ||
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) { | ||
let mut applicability = Applicability::MachineApplicable; | ||
|
||
if let ExprKind::Binary(bin_op, left, right) = expr.kind | ||
&& bin_op.node == BinOpKind::Eq | ||
{ | ||
// a.count_ones() == 1 | ||
if let ExprKind::MethodCall(method_name, reciever, _, _) = left.kind | ||
&& method_name.ident.as_str() == "count_ones" | ||
&& let &Uint(_) = cx.typeck_results().expr_ty(reciever).kind() | ||
&& check_lit(right, 1) | ||
{ | ||
build_sugg(cx, expr, reciever, &mut applicability); | ||
} | ||
|
||
// 1 == a.count_ones() | ||
if let ExprKind::MethodCall(method_name, reciever, _, _) = right.kind | ||
&& method_name.ident.as_str() == "count_ones" | ||
&& let &Uint(_) = cx.typeck_results().expr_ty(reciever).kind() | ||
&& check_lit(left, 1) | ||
{ | ||
build_sugg(cx, expr, reciever, &mut applicability); | ||
} | ||
|
||
// a & (a - 1) == 0 | ||
if let ExprKind::Binary(op1, left1, right1) = left.kind | ||
&& op1.node == BinOpKind::BitAnd | ||
&& let ExprKind::Binary(op2, left2, right2) = right1.kind | ||
&& op2.node == BinOpKind::Sub | ||
&& check_eq_expr(cx, left1, left2) | ||
&& let &Uint(_) = cx.typeck_results().expr_ty(left1).kind() | ||
&& check_lit(right2, 1) | ||
&& check_lit(right, 0) | ||
{ | ||
build_sugg(cx, expr, left1, &mut applicability); | ||
} | ||
|
||
// (a - 1) & a == 0; | ||
if let ExprKind::Binary(op1, left1, right1) = left.kind | ||
&& op1.node == BinOpKind::BitAnd | ||
&& let ExprKind::Binary(op2, left2, right2) = left1.kind | ||
&& op2.node == BinOpKind::Sub | ||
&& check_eq_expr(cx, right1, left2) | ||
&& let &Uint(_) = cx.typeck_results().expr_ty(right1).kind() | ||
&& check_lit(right2, 1) | ||
&& check_lit(right, 0) | ||
{ | ||
build_sugg(cx, expr, right1, &mut applicability); | ||
} | ||
|
||
// 0 == a & (a - 1); | ||
if let ExprKind::Binary(op1, left1, right1) = right.kind | ||
&& op1.node == BinOpKind::BitAnd | ||
&& let ExprKind::Binary(op2, left2, right2) = right1.kind | ||
&& op2.node == BinOpKind::Sub | ||
&& check_eq_expr(cx, left1, left2) | ||
&& let &Uint(_) = cx.typeck_results().expr_ty(left1).kind() | ||
&& check_lit(right2, 1) | ||
&& check_lit(left, 0) | ||
{ | ||
build_sugg(cx, expr, left1, &mut applicability); | ||
} | ||
|
||
// 0 == (a - 1) & a | ||
if let ExprKind::Binary(op1, left1, right1) = right.kind | ||
&& op1.node == BinOpKind::BitAnd | ||
&& let ExprKind::Binary(op2, left2, right2) = left1.kind | ||
&& op2.node == BinOpKind::Sub | ||
&& check_eq_expr(cx, right1, left2) | ||
&& let &Uint(_) = cx.typeck_results().expr_ty(right1).kind() | ||
&& check_lit(right2, 1) | ||
&& check_lit(left, 0) | ||
{ | ||
build_sugg(cx, expr, right1, &mut applicability); | ||
} | ||
} | ||
} | ||
} | ||
|
||
fn build_sugg(cx: &LateContext<'_>, expr: &Expr<'_>, reciever: &Expr<'_>, applicability: &mut Applicability) { | ||
let snippet = snippet_with_applicability(cx, reciever.span, "..", applicability); | ||
|
||
span_lint_and_sugg( | ||
cx, | ||
MANUAL_IS_POWER_OF_TWO, | ||
expr.span, | ||
"manually reimplementing `is_power_of_two`", | ||
"consider using `.is_power_of_two()`", | ||
format!("{snippet}.is_power_of_two()"), | ||
*applicability, | ||
); | ||
} | ||
|
||
fn check_lit(expr: &Expr<'_>, expected_num: u128) -> bool { | ||
if let ExprKind::Lit(lit) = expr.kind | ||
&& let LitKind::Int(Pu128(num), _) = lit.node | ||
&& num == expected_num | ||
{ | ||
return true; | ||
} | ||
false | ||
} | ||
|
||
fn check_eq_expr(cx: &LateContext<'_>, lhs: &Expr<'_>, rhs: &Expr<'_>) -> bool { | ||
SpanlessEq::new(cx).eq_expr(lhs, rhs) | ||
} |
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,20 @@ | ||
#![warn(clippy::manual_is_power_of_two)] | ||
|
||
fn main() { | ||
let a = 16_u64; | ||
|
||
let _ = a.is_power_of_two(); | ||
let _ = a.is_power_of_two(); | ||
|
||
// Test different orders of expression | ||
let _ = a.is_power_of_two(); | ||
let _ = a.is_power_of_two(); | ||
let _ = a.is_power_of_two(); | ||
let _ = a.is_power_of_two(); | ||
|
||
let b = 4_i64; | ||
|
||
// is_power_of_two only works for unsigned integers | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might be good idea mentioned about this at lint doc as well |
||
let _ = b.count_ones() == 1; | ||
let _ = b & (b - 1) == 0; | ||
} |
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,20 @@ | ||
#![warn(clippy::manual_is_power_of_two)] | ||
|
||
fn main() { | ||
let a = 16_u64; | ||
|
||
let _ = a.count_ones() == 1; | ||
let _ = a & (a - 1) == 0; | ||
|
||
Sour1emon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Test different orders of expression | ||
let _ = 1 == a.count_ones(); | ||
let _ = (a - 1) & a == 0; | ||
let _ = 0 == a & (a - 1); | ||
let _ = 0 == (a - 1) & a; | ||
|
||
let b = 4_i64; | ||
|
||
// is_power_of_two only works for unsigned integers | ||
let _ = b.count_ones() == 1; | ||
let _ = b & (b - 1) == 0; | ||
} |
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,41 @@ | ||
error: manually reimplementing `is_power_of_two` | ||
--> tests/ui/manual_is_power_of_two.rs:6:13 | ||
| | ||
LL | let _ = a.count_ones() == 1; | ||
| ^^^^^^^^^^^^^^^^^^^ help: consider using `.is_power_of_two()`: `a.is_power_of_two()` | ||
| | ||
= note: `-D clippy::manual-is-power-of-two` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::manual_is_power_of_two)]` | ||
|
||
error: manually reimplementing `is_power_of_two` | ||
--> tests/ui/manual_is_power_of_two.rs:7:13 | ||
| | ||
LL | let _ = a & (a - 1) == 0; | ||
| ^^^^^^^^^^^^^^^^ help: consider using `.is_power_of_two()`: `a.is_power_of_two()` | ||
|
||
error: manually reimplementing `is_power_of_two` | ||
--> tests/ui/manual_is_power_of_two.rs:10:13 | ||
| | ||
LL | let _ = 1 == a.count_ones(); | ||
| ^^^^^^^^^^^^^^^^^^^ help: consider using `.is_power_of_two()`: `a.is_power_of_two()` | ||
|
||
error: manually reimplementing `is_power_of_two` | ||
--> tests/ui/manual_is_power_of_two.rs:11:13 | ||
| | ||
LL | let _ = (a - 1) & a == 0; | ||
| ^^^^^^^^^^^^^^^^ help: consider using `.is_power_of_two()`: `a.is_power_of_two()` | ||
|
||
error: manually reimplementing `is_power_of_two` | ||
--> tests/ui/manual_is_power_of_two.rs:12:13 | ||
| | ||
LL | let _ = 0 == a & (a - 1); | ||
| ^^^^^^^^^^^^^^^^ help: consider using `.is_power_of_two()`: `a.is_power_of_two()` | ||
|
||
error: manually reimplementing `is_power_of_two` | ||
--> tests/ui/manual_is_power_of_two.rs:13:13 | ||
| | ||
LL | let _ = 0 == (a - 1) & a; | ||
| ^^^^^^^^^^^^^^^^ help: consider using `.is_power_of_two()`: `a.is_power_of_two()` | ||
|
||
error: aborting due to 6 previous errors | ||
|
Oops, something went wrong.
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.
You can loop over [(left, right), (right, left)] to reduce the code duplication here and elsewhere.