-
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
e9c2d82
commit f28fbf1
Showing
15 changed files
with
265 additions
and
27 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::msrvs::{self, Msrv}; | ||
use clippy_utils::sugg::Sugg; | ||
use clippy_utils::{is_floating_point_integer_literal, is_integer_literal}; | ||
use rustc_ast::BinOpKind; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::{Expr, ExprKind}; | ||
use rustc_lint::LateContext; | ||
use rustc_middle::ty; | ||
|
||
use super::MANUAL_MIDPOINT; | ||
|
||
pub(super) fn check<'tcx>( | ||
cx: &LateContext<'tcx>, | ||
expr: &'tcx Expr<'_>, | ||
op: BinOpKind, | ||
left: &'tcx Expr<'_>, | ||
right: &'tcx Expr<'_>, | ||
msrv: &Msrv, | ||
) { | ||
if msrv.meets(msrvs::UINT_FLOAT_MIDPOINT) | ||
&& !left.span.from_expansion() | ||
&& !right.span.from_expansion() | ||
&& op == BinOpKind::Div | ||
&& (is_integer_literal(right, 2) || is_floating_point_integer_literal(right, 2)) | ||
&& let Some((ll_expr, lr_expr)) = add_operands(left) | ||
&& add_operands(ll_expr).is_none() && add_operands(lr_expr).is_none() | ||
&& let left_ty = cx.typeck_results().expr_ty_adjusted(ll_expr) | ||
&& let right_ty = cx.typeck_results().expr_ty_adjusted(lr_expr) | ||
&& left_ty == right_ty | ||
// FIXME: Also lint on signed integers when rust-lang/rust#134340 is merged | ||
&& matches!(left_ty.kind(), ty::Uint(_) | ty::Float(_)) | ||
{ | ||
let mut app = Applicability::MachineApplicable; | ||
let left_sugg = Sugg::hir_with_context(cx, ll_expr, expr.span.ctxt(), "..", &mut app); | ||
let right_sugg = Sugg::hir_with_context(cx, lr_expr, expr.span.ctxt(), "..", &mut app); | ||
let sugg = format!("{left_ty}::midpoint({left_sugg}, {right_sugg})"); | ||
span_lint_and_sugg( | ||
cx, | ||
MANUAL_MIDPOINT, | ||
expr.span, | ||
"manual implementation of `midpoint` which can overflow", | ||
format!("use `{left_ty}::midpoint` instead"), | ||
sugg, | ||
app, | ||
); | ||
} | ||
} | ||
|
||
/// Return the left and right operands if `expr` represents an addition | ||
fn add_operands<'e, 'tcx>(expr: &'e Expr<'tcx>) -> Option<(&'e Expr<'tcx>, &'e Expr<'tcx>)> { | ||
match expr.kind { | ||
ExprKind::Binary(op, left, right) if op.node == BinOpKind::Add => Some((left, right)), | ||
_ => 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,48 @@ | ||
#![warn(clippy::manual_midpoint)] | ||
|
||
macro_rules! mac { | ||
($a: expr, $b: expr) => { | ||
($a + $b) / 2 | ||
}; | ||
} | ||
|
||
macro_rules! add { | ||
($a: expr, $b: expr) => { | ||
($a + $b) | ||
}; | ||
} | ||
|
||
macro_rules! two { | ||
() => { | ||
2 | ||
}; | ||
} | ||
|
||
fn main() { | ||
let a: u32 = 10; | ||
let _ = u32::midpoint(a, 5); //~ ERROR: manual implementation of `midpoint` | ||
|
||
let f: f32 = 10.0; | ||
let _ = f32::midpoint(f, 5.0); //~ ERROR: manual implementation of `midpoint` | ||
|
||
let _: u32 = 5 + u32::midpoint(8, 8) + 2; //~ ERROR: manual implementation of `midpoint` | ||
let _: u32 = const { u32::midpoint(8, 8) }; //~ ERROR: manual implementation of `midpoint` | ||
let _: f64 = const { f64::midpoint(8.0f64, 8.) }; //~ ERROR: manual implementation of `midpoint` | ||
let _: u32 = u32::midpoint(u32::default(), u32::default()); //~ ERROR: manual implementation of `midpoint` | ||
let _: u32 = u32::midpoint(two!(), two!()); //~ ERROR: manual implementation of `midpoint` | ||
|
||
// Do not lint in presence of an addition with more than 2 operands | ||
let _: u32 = (10 + 20 + 30) / 2; | ||
|
||
// Do not lint if whole or part is coming from a macro | ||
let _ = mac!(10, 20); | ||
let _: u32 = add!(10u32, 20u32) / 2; | ||
let _: u32 = (10 + 20) / two!(); | ||
|
||
// Do not lint if a literal is not present | ||
let _ = (f + 5.0) / (1.0 + 1.0); | ||
|
||
// Do not lint on signed integer types | ||
let i: i32 = 10; | ||
let _ = (i + 5) / 2; | ||
} |
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,48 @@ | ||
#![warn(clippy::manual_midpoint)] | ||
|
||
macro_rules! mac { | ||
($a: expr, $b: expr) => { | ||
($a + $b) / 2 | ||
}; | ||
} | ||
|
||
macro_rules! add { | ||
($a: expr, $b: expr) => { | ||
($a + $b) | ||
}; | ||
} | ||
|
||
macro_rules! two { | ||
() => { | ||
2 | ||
}; | ||
} | ||
|
||
fn main() { | ||
let a: u32 = 10; | ||
let _ = (a + 5) / 2; //~ ERROR: manual implementation of `midpoint` | ||
|
||
let f: f32 = 10.0; | ||
let _ = (f + 5.0) / 2.0; //~ ERROR: manual implementation of `midpoint` | ||
|
||
let _: u32 = 5 + (8 + 8) / 2 + 2; //~ ERROR: manual implementation of `midpoint` | ||
let _: u32 = const { (8 + 8) / 2 }; //~ ERROR: manual implementation of `midpoint` | ||
let _: f64 = const { (8.0f64 + 8.) / 2. }; //~ ERROR: manual implementation of `midpoint` | ||
let _: u32 = (u32::default() + u32::default()) / 2; //~ ERROR: manual implementation of `midpoint` | ||
let _: u32 = (two!() + two!()) / 2; //~ ERROR: manual implementation of `midpoint` | ||
|
||
// Do not lint in presence of an addition with more than 2 operands | ||
let _: u32 = (10 + 20 + 30) / 2; | ||
|
||
// Do not lint if whole or part is coming from a macro | ||
let _ = mac!(10, 20); | ||
let _: u32 = add!(10u32, 20u32) / 2; | ||
let _: u32 = (10 + 20) / two!(); | ||
|
||
// Do not lint if a literal is not present | ||
let _ = (f + 5.0) / (1.0 + 1.0); | ||
|
||
// Do not lint on signed integer types | ||
let i: i32 = 10; | ||
let _ = (i + 5) / 2; | ||
} |
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,47 @@ | ||
error: manual implementation of `midpoint` which can overflow | ||
--> tests/ui/manual_midpoint.rs:23:13 | ||
| | ||
LL | let _ = (a + 5) / 2; | ||
| ^^^^^^^^^^^ help: use `u32::midpoint` instead: `u32::midpoint(a, 5)` | ||
| | ||
= note: `-D clippy::manual-midpoint` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::manual_midpoint)]` | ||
|
||
error: manual implementation of `midpoint` which can overflow | ||
--> tests/ui/manual_midpoint.rs:26:13 | ||
| | ||
LL | let _ = (f + 5.0) / 2.0; | ||
| ^^^^^^^^^^^^^^^ help: use `f32::midpoint` instead: `f32::midpoint(f, 5.0)` | ||
|
||
error: manual implementation of `midpoint` which can overflow | ||
--> tests/ui/manual_midpoint.rs:28:22 | ||
| | ||
LL | let _: u32 = 5 + (8 + 8) / 2 + 2; | ||
| ^^^^^^^^^^^ help: use `u32::midpoint` instead: `u32::midpoint(8, 8)` | ||
|
||
error: manual implementation of `midpoint` which can overflow | ||
--> tests/ui/manual_midpoint.rs:29:26 | ||
| | ||
LL | let _: u32 = const { (8 + 8) / 2 }; | ||
| ^^^^^^^^^^^ help: use `u32::midpoint` instead: `u32::midpoint(8, 8)` | ||
|
||
error: manual implementation of `midpoint` which can overflow | ||
--> tests/ui/manual_midpoint.rs:30:26 | ||
| | ||
LL | let _: f64 = const { (8.0f64 + 8.) / 2. }; | ||
| ^^^^^^^^^^^^^^^^^^ help: use `f64::midpoint` instead: `f64::midpoint(8.0f64, 8.)` | ||
|
||
error: manual implementation of `midpoint` which can overflow | ||
--> tests/ui/manual_midpoint.rs:31:18 | ||
| | ||
LL | let _: u32 = (u32::default() + u32::default()) / 2; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `u32::midpoint` instead: `u32::midpoint(u32::default(), u32::default())` | ||
|
||
error: manual implementation of `midpoint` which can overflow | ||
--> tests/ui/manual_midpoint.rs:32:18 | ||
| | ||
LL | let _: u32 = (two!() + two!()) / 2; | ||
| ^^^^^^^^^^^^^^^^^^^^^ help: use `u32::midpoint` instead: `u32::midpoint(two!(), two!())` | ||
|
||
error: aborting due to 7 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
Oops, something went wrong.