Skip to content

Commit 588cd65

Browse files
committed
Fix manual_div_ceil to support literals
1 parent 336dac2 commit 588cd65

File tree

4 files changed

+21
-21
lines changed

4 files changed

+21
-21
lines changed

clippy_lints/src/manual_div_ceil.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use clippy_utils::SpanlessEq;
44
use rustc_ast::{BinOpKind, LitKind};
55
use rustc_data_structures::packed::Pu128;
66
use rustc_errors::Applicability;
7-
use rustc_hir::{Expr, ExprKind, QPath};
7+
use rustc_hir::{Expr, ExprKind};
88
use rustc_lint::{LateContext, LateLintPass};
99
use rustc_middle::ty::{self};
1010
use rustc_session::declare_lint_pass;
@@ -53,7 +53,7 @@ impl LateLintPass<'_> for ManualDivCeil {
5353
&& let ExprKind::Binary(sub_op, sub_lhs, sub_rhs) = add_rhs.kind
5454
&& sub_op.node == BinOpKind::Sub
5555
&& check_literal(sub_rhs)
56-
&& check_eq_path_segment(cx, sub_lhs, div_rhs)
56+
&& check_eq_expr(cx, sub_lhs, div_rhs)
5757
{
5858
build_suggestion(cx, expr, add_lhs, div_rhs, &mut applicability);
5959
return;
@@ -65,7 +65,7 @@ impl LateLintPass<'_> for ManualDivCeil {
6565
&& let ExprKind::Binary(sub_op, sub_lhs, sub_rhs) = add_lhs.kind
6666
&& sub_op.node == BinOpKind::Sub
6767
&& check_literal(sub_rhs)
68-
&& check_eq_path_segment(cx, sub_lhs, div_rhs)
68+
&& check_eq_expr(cx, sub_lhs, div_rhs)
6969
{
7070
build_suggestion(cx, expr, add_rhs, div_rhs, &mut applicability);
7171
return;
@@ -77,7 +77,7 @@ impl LateLintPass<'_> for ManualDivCeil {
7777
&& let ExprKind::Binary(add_op, add_lhs, add_rhs) = sub_lhs.kind
7878
&& add_op.node == BinOpKind::Add
7979
&& check_literal(sub_rhs)
80-
&& check_eq_path_segment(cx, add_rhs, div_rhs)
80+
&& check_eq_expr(cx, add_rhs, div_rhs)
8181
{
8282
build_suggestion(cx, expr, add_lhs, div_rhs, &mut applicability);
8383
}
@@ -99,14 +99,8 @@ fn check_literal(expr: &Expr<'_>) -> bool {
9999
false
100100
}
101101

102-
fn check_eq_path_segment(cx: &LateContext<'_>, lhs: &Expr<'_>, rhs: &Expr<'_>) -> bool {
103-
if let ExprKind::Path(QPath::Resolved(_, lhs_path)) = lhs.kind
104-
&& let ExprKind::Path(QPath::Resolved(_, rhs_path)) = rhs.kind
105-
&& SpanlessEq::new(cx).eq_path_segment(&lhs_path.segments[0], &rhs_path.segments[0])
106-
{
107-
return true;
108-
}
109-
false
102+
fn check_eq_expr(cx: &LateContext<'_>, lhs: &Expr<'_>, rhs: &Expr<'_>) -> bool {
103+
SpanlessEq::new(cx).eq_expr(lhs, rhs)
110104
}
111105

112106
fn build_suggestion(

tests/ui/manual_div_ceil.fixed

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22
#![feature(int_roundings)]
33

44
fn main() {
5-
let x: i32 = 7;
6-
let y: i32 = 4;
5+
let x = 7_i32;
6+
let y = 4_i32;
77

88
// Lint.
99
let _ = x.div_ceil(y);
1010
let _ = x.div_ceil(y);
1111
let _ = x.div_ceil(y);
1212

13-
let _ = (7 + (4 - 1)) / 4;
13+
let _ = 7_i32.div_ceil(4);
1414

1515
// No lint.
1616
let _ = (x + (y - 2)) / y;
1717
let _ = (x + (y + 1)) / y;
1818

19-
let z: i32 = 3;
19+
let z = 3_i32;
2020
let _ = (x + (y - 1)) / z;
2121
}

tests/ui/manual_div_ceil.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22
#![feature(int_roundings)]
33

44
fn main() {
5-
let x: i32 = 7;
6-
let y: i32 = 4;
5+
let x = 7_i32;
6+
let y = 4_i32;
77

88
// Lint.
99
let _ = (x + (y - 1)) / y;
1010
let _ = ((y - 1) + x) / y;
1111
let _ = (x + y - 1) / y;
1212

13-
let _ = (7 + (4 - 1)) / 4;
13+
let _ = (7_i32 + (4 - 1)) / 4;
1414

1515
// No lint.
1616
let _ = (x + (y - 2)) / y;
1717
let _ = (x + (y + 1)) / y;
1818

19-
let z: i32 = 3;
19+
let z = 3_i32;
2020
let _ = (x + (y - 1)) / z;
2121
}

tests/ui/manual_div_ceil.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,11 @@ error: manually reimplementing `div_ceil`
1919
LL | let _ = (x + y - 1) / y;
2020
| ^^^^^^^^^^^^^^^ help: consider using `.div_ceil()`: `x.div_ceil(y)`
2121

22-
error: aborting due to 3 previous errors
22+
error: manually reimplementing `div_ceil`
23+
--> tests/ui/manual_div_ceil.rs:13:13
24+
|
25+
LL | let _ = (7_i32 + (4 - 1)) / 4;
26+
| ^^^^^^^^^^^^^^^^^^^^^ help: consider using `.div_ceil()`: `7_i32.div_ceil(4)`
27+
28+
error: aborting due to 4 previous errors
2329

0 commit comments

Comments
 (0)