-
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 906f3bd
Showing
13 changed files
with
164 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
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) | ||
&& op == BinOpKind::Div | ||
&& (is_integer_literal(right, 2) || is_floating_point_integer_literal(right, 2)) | ||
&& let ExprKind::Binary(left_op, ll_expr, lr_expr) = left.kind | ||
&& left_op.node == BinOpKind::Add | ||
&& 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 | ||
&& matches!(left_ty.kind(), ty::Uint(_) | ty::Float(_)) | ||
{ | ||
let mut app = Applicability::MachineApplicable; | ||
let left_sugg = Sugg::hir_with_applicability(cx, ll_expr, "..", &mut app); | ||
let right_sugg = Sugg::hir_with_applicability(cx, lr_expr, "..", &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`", | ||
"use instead", | ||
sugg, | ||
app, | ||
); | ||
} | ||
} |
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,19 @@ | ||
#![warn(clippy::manual_midpoint)] | ||
|
||
macro_rules! mac { | ||
($a: expr, $b: expr) => {{ ($a + $b) / 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` | ||
|
||
// Do not lint if a literal is not present | ||
let _ = (f + 5.0) / (1.0 + 1.0); | ||
|
||
// Do not lint | ||
let _ = mac!(10, 20); | ||
} |
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,19 @@ | ||
#![warn(clippy::manual_midpoint)] | ||
|
||
macro_rules! mac { | ||
($a: expr, $b: expr) => {{ ($a + $b) / 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` | ||
|
||
// Do not lint if a literal is not present | ||
let _ = (f + 5.0) / (1.0 + 1.0); | ||
|
||
// Do not lint | ||
let _ = mac!(10, 20); | ||
} |
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 `midpoint` | ||
--> tests/ui/manual_midpoint.rs:9:13 | ||
| | ||
LL | let _ = (a + 5) / 2; | ||
| ^^^^^^^^^^^ help: use 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` | ||
--> tests/ui/manual_midpoint.rs:12:13 | ||
| | ||
LL | let _ = (f + 5.0) / 2.0; | ||
| ^^^^^^^^^^^^^^^ help: use instead: `f32::midpoint(f, 5.0)` | ||
|
||
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
Oops, something went wrong.