-
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
1 parent
9a0a658
commit b5db45e
Showing
17 changed files
with
375 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
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,115 @@ | ||
use clippy_utils::consts::{integer_const, is_zero_integer_const}; | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::msrvs::{self, Msrv}; | ||
use clippy_utils::source::SpanRangeExt; | ||
use clippy_utils::sugg::Sugg; | ||
use rustc_ast::BinOpKind; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::{Expr, ExprKind}; | ||
use rustc_lint::LateContext; | ||
use rustc_middle::ty; | ||
|
||
use super::MANUAL_IS_MULTIPLE_OF; | ||
|
||
pub(super) fn check<'tcx>( | ||
cx: &LateContext<'tcx>, | ||
expr: &Expr<'_>, | ||
op: BinOpKind, | ||
lhs: &'tcx Expr<'tcx>, | ||
rhs: &'tcx Expr<'tcx>, | ||
min_and_mask_size: u8, | ||
msrv: &Msrv, | ||
) { | ||
if let Some(operand) = uint_compare_to_zero(cx, op, lhs, rhs) | ||
&& let ExprKind::Binary(lhs_op, lhs_left, lhs_right) = operand.kind | ||
&& msrv.meets(msrvs::UNSIGNED_IS_MULTIPLE_OF) | ||
{ | ||
let mut app = Applicability::MachineApplicable; | ||
let (dividend, divisor) = if lhs_op.node == BinOpKind::Rem { | ||
( | ||
lhs_left, | ||
Sugg::hir_with_applicability(cx, lhs_right, "_", &mut app).into_string(), | ||
) | ||
} else if lhs_op.node == BinOpKind::BitAnd { | ||
let min_divisor = 1 << u128::from(min_and_mask_size); | ||
if let Some(divisor) = is_all_ones(cx, lhs_right, min_divisor, &mut app) { | ||
(lhs_left, divisor) | ||
} else if let Some(divisor) = is_all_ones(cx, lhs_left, min_divisor, &mut app) { | ||
(lhs_right, divisor) | ||
} else { | ||
return; | ||
} | ||
} else { | ||
return; | ||
}; | ||
span_lint_and_sugg( | ||
cx, | ||
MANUAL_IS_MULTIPLE_OF, | ||
expr.span, | ||
"manual implementation of `.is_multiple_of()`", | ||
"replace with", | ||
format!( | ||
"{}{}.is_multiple_of({divisor})", | ||
if op == BinOpKind::Eq { "" } else { "!" }, | ||
Sugg::hir_with_applicability(cx, dividend, "_", &mut app).maybe_par() | ||
), | ||
app, | ||
); | ||
} | ||
} | ||
|
||
// If we have a `x == 0`, `x != 0` or `x > 0` (or the reverted ones), return the non-zero operand | ||
fn uint_compare_to_zero<'tcx>( | ||
cx: &LateContext<'tcx>, | ||
op: BinOpKind, | ||
lhs: &'tcx Expr<'tcx>, | ||
rhs: &'tcx Expr<'tcx>, | ||
) -> Option<&'tcx Expr<'tcx>> { | ||
let operand = if matches!(lhs.kind, ExprKind::Binary(..)) | ||
&& matches!(op, BinOpKind::Eq | BinOpKind::Ne | BinOpKind::Gt) | ||
&& is_zero_integer_const(cx, rhs) | ||
{ | ||
lhs | ||
} else if matches!(rhs.kind, ExprKind::Binary(..)) | ||
&& matches!(op, BinOpKind::Eq | BinOpKind::Ne | BinOpKind::Lt) | ||
&& is_zero_integer_const(cx, lhs) | ||
{ | ||
rhs | ||
} else { | ||
return None; | ||
}; | ||
|
||
matches!(cx.typeck_results().expr_ty_adjusted(operand).kind(), ty::Uint(_)).then_some(operand) | ||
} | ||
|
||
/// If `expr` is made of all ones, return the representation of `expr+1` if it is no smaller than | ||
/// `min_divisor`. | ||
fn is_all_ones<'tcx>( | ||
cx: &LateContext<'tcx>, | ||
expr: &'tcx Expr<'tcx>, | ||
min_divisor: u128, | ||
app: &mut Applicability, | ||
) -> Option<String> { | ||
if let ExprKind::Binary(op, lhs, rhs) = expr.kind | ||
&& op.node == BinOpKind::Sub | ||
&& let ExprKind::Binary(op, lhs_left, lhs_right) = lhs.kind | ||
&& op.node == BinOpKind::Shl | ||
&& let Some(1) = integer_const(cx, lhs_left) | ||
&& let Some(1) = integer_const(cx, rhs) | ||
&& integer_const(cx, lhs_right).is_none_or(|v| 1 << v >= min_divisor) | ||
{ | ||
Some(Sugg::hir_with_applicability(cx, lhs, "_", app).to_string()) | ||
} else if let Some(value) = integer_const(cx, expr) | ||
&& let Some(inc_value) = value.checked_add(1) | ||
&& inc_value.is_power_of_two() | ||
{ | ||
let repr = if expr.span.check_source_text(cx, |s| s.starts_with("0x")) { | ||
format!("{inc_value:#x}") | ||
} else { | ||
inc_value.to_string() | ||
}; | ||
(inc_value >= min_divisor).then_some(repr) | ||
} else { | ||
None | ||
} | ||
} |
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 @@ | ||
min-and-mask-size = 0 |
9 changes: 9 additions & 0 deletions
9
tests/ui-toml/manual_is_multiple_of/manual_is_multiple_of.fixed
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,9 @@ | ||
#![warn(clippy::manual_is_multiple_of)] | ||
#![feature(unsigned_is_multiple_of)] | ||
|
||
fn main() {} | ||
|
||
fn f(a: u64, b: u64) { | ||
let _ = a.is_multiple_of(2); //~ manual_is_multiple_of | ||
let _ = a.is_multiple_of(1 << 1); //~ manual_is_multiple_of | ||
} |
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,9 @@ | ||
#![warn(clippy::manual_is_multiple_of)] | ||
#![feature(unsigned_is_multiple_of)] | ||
|
||
fn main() {} | ||
|
||
fn f(a: u64, b: u64) { | ||
let _ = a & 1 == 0; //~ manual_is_multiple_of | ||
let _ = a & ((1 << 1) - 1) == 0; //~ manual_is_multiple_of | ||
} |
17 changes: 17 additions & 0 deletions
17
tests/ui-toml/manual_is_multiple_of/manual_is_multiple_of.stderr
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,17 @@ | ||
error: manual implementation of `.is_multiple_of()` | ||
--> tests/ui-toml/manual_is_multiple_of/manual_is_multiple_of.rs:7:13 | ||
| | ||
LL | let _ = a & 1 == 0; | ||
| ^^^^^^^^^^ help: replace with: `a.is_multiple_of(2)` | ||
| | ||
= note: `-D clippy::manual-is-multiple-of` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::manual_is_multiple_of)]` | ||
|
||
error: manual implementation of `.is_multiple_of()` | ||
--> tests/ui-toml/manual_is_multiple_of/manual_is_multiple_of.rs:8:13 | ||
| | ||
LL | let _ = a & ((1 << 1) - 1) == 0; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `a.is_multiple_of(1 << 1)` | ||
|
||
error: aborting due to 2 previous errors | ||
|
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,33 @@ | ||
#![warn(clippy::manual_is_multiple_of)] | ||
#![feature(unsigned_is_multiple_of)] | ||
|
||
fn main() {} | ||
|
||
#[clippy::msrv = "1.87"] | ||
fn f(a: u64, b: u64) { | ||
let _ = a.is_multiple_of(b); //~ manual_is_multiple_of | ||
let _ = (a + 1).is_multiple_of(b + 1); //~ manual_is_multiple_of | ||
let _ = !a.is_multiple_of(b); //~ manual_is_multiple_of | ||
let _ = !(a + 1).is_multiple_of(b + 1); //~ manual_is_multiple_of | ||
|
||
let _ = a.is_multiple_of(4096); //~ manual_is_multiple_of | ||
let _ = !a.is_multiple_of(4096); //~ manual_is_multiple_of | ||
let _ = a.is_multiple_of(1 << b); //~ manual_is_multiple_of | ||
let _ = !a.is_multiple_of(1 << b); //~ manual_is_multiple_of | ||
let _ = a.is_multiple_of(1 << b); //~ manual_is_multiple_of | ||
|
||
let _ = !a.is_multiple_of(b); //~ manual_is_multiple_of | ||
let _ = !a.is_multiple_of(b); //~ manual_is_multiple_of | ||
|
||
let _ = a.is_multiple_of(0x100); //~ manual_is_multiple_of | ||
|
||
let _ = a & 1 == 0; // Do not lint: below `min-and-mask-size` | ||
let _ = a & ((1 << 1) - 1) == 0; // Do not lint: below `min-and-mask-size` | ||
let _ = a.is_multiple_of(8); //~ manual_is_multiple_of | ||
let _ = a.is_multiple_of(1 << 3); //~ manual_is_multiple_of | ||
} | ||
|
||
#[clippy::msrv = "1.86"] | ||
fn g(a: u64, b: u64) { | ||
let _ = a % b == 0; | ||
} |
Oops, something went wrong.