Skip to content

Commit 39a4086

Browse files
authored
manual_div_ceil: fix suggestions when macro is involved (#14666)
here is my small fix changelog: fix suggestion span to avoid showing macro name in `.div_ceil()` suggestion i changed this line `let divisor_snippet = snippet_with_applicability(cx, rhs.spansource_callsite(), "..", applicability);` to this line `let divisor_snippet = snippet_with_applicability(cx, rhs.span, "..", applicability);` to fix problem where this warning in macro expands like this ```rust 4 | let _ = (x + 7) / 8; | ^^^^^^^^^^^ help: consider using `.div_ceil()`: `x.div_ceil(y!())` ``` [play it yourself](https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=397aa8cd2ffffb24a286fbddbc75446c) as you can see here it looks like `x.div_ceil(y!())` and contains macro signature so i fixed this problem, i will look closely if there any more problems like this and fix them in order **Related issue** fixes [#14665](#14665)
2 parents 58cfdb7 + fc12b5b commit 39a4086

File tree

4 files changed

+94
-20
lines changed

4 files changed

+94
-20
lines changed

clippy_lints/src/manual_div_ceil.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_config::Conf;
22
use clippy_utils::diagnostics::span_lint_and_sugg;
33
use clippy_utils::msrvs::{self, Msrv};
4-
use clippy_utils::source::snippet_with_applicability;
4+
use clippy_utils::source::snippet_with_context;
55
use clippy_utils::sugg::{Sugg, has_enclosing_paren};
66
use clippy_utils::{SpanlessEq, sym};
77
use rustc_ast::{BinOpKind, LitIntType, LitKind, UnOp};
@@ -199,9 +199,9 @@ fn build_suggestion(
199199
} else {
200200
format!("{dividend_sugg_str}{type_suffix}")
201201
};
202-
let divisor_snippet = snippet_with_applicability(cx, rhs.span.source_callsite(), "..", applicability);
202+
let divisor_snippet = snippet_with_context(cx, rhs.span, expr.span.ctxt(), "..", applicability);
203203

204-
let sugg = format!("{suggestion_before_div_ceil}.div_ceil({divisor_snippet})");
204+
let sugg = format!("{suggestion_before_div_ceil}.div_ceil({})", divisor_snippet.0);
205205

206206
span_lint_and_sugg(
207207
cx,

tests/ui/manual_div_ceil.fixed

+23
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
#![warn(clippy::manual_div_ceil)]
22

3+
macro_rules! y {
4+
() => {
5+
let x = 33u32;
6+
let _ = x.div_ceil(8);
7+
//~^ manual_div_ceil
8+
let _ = x.div_ceil(8);
9+
//~^ manual_div_ceil
10+
};
11+
}
12+
13+
macro_rules! eight {
14+
() => {
15+
8
16+
};
17+
}
18+
319
fn main() {
420
let x = 7_u32;
521
let y = 4_u32;
@@ -32,6 +48,13 @@ fn main() {
3248
let _ = (z as i32 + (y_i - 1)) / y_i;
3349
let _ = (7_u32 as i32 + (y_i - 1)) / y_i;
3450
let _ = (7_u32 as i32 + (4 - 1)) / 4;
51+
52+
// Test lint with macro
53+
y!();
54+
55+
// Also test if RHS should be result of macro expansion
56+
let _ = 33u32.div_ceil(eight!());
57+
//~^ manual_div_ceil
3558
}
3659

3760
fn issue_13843() {

tests/ui/manual_div_ceil.rs

+23
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
#![warn(clippy::manual_div_ceil)]
22

3+
macro_rules! y {
4+
() => {
5+
let x = 33u32;
6+
let _ = (x + 7) / 8;
7+
//~^ manual_div_ceil
8+
let _ = (7 + x) / 8;
9+
//~^ manual_div_ceil
10+
};
11+
}
12+
13+
macro_rules! eight {
14+
() => {
15+
8
16+
};
17+
}
18+
319
fn main() {
420
let x = 7_u32;
521
let y = 4_u32;
@@ -32,6 +48,13 @@ fn main() {
3248
let _ = (z as i32 + (y_i - 1)) / y_i;
3349
let _ = (7_u32 as i32 + (y_i - 1)) / y_i;
3450
let _ = (7_u32 as i32 + (4 - 1)) / 4;
51+
52+
// Test lint with macro
53+
y!();
54+
55+
// Also test if RHS should be result of macro expansion
56+
let _ = (33u32 + 7) / eight!();
57+
//~^ manual_div_ceil
3558
}
3659

3760
fn issue_13843() {

tests/ui/manual_div_ceil.stderr

+45-17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: manually reimplementing `div_ceil`
2-
--> tests/ui/manual_div_ceil.rs:9:13
2+
--> tests/ui/manual_div_ceil.rs:25:13
33
|
44
LL | let _ = (x + (y - 1)) / y;
55
| ^^^^^^^^^^^^^^^^^ help: consider using `.div_ceil()`: `x.div_ceil(y)`
@@ -8,94 +8,122 @@ LL | let _ = (x + (y - 1)) / y;
88
= help: to override `-D warnings` add `#[allow(clippy::manual_div_ceil)]`
99

1010
error: manually reimplementing `div_ceil`
11-
--> tests/ui/manual_div_ceil.rs:11:13
11+
--> tests/ui/manual_div_ceil.rs:27:13
1212
|
1313
LL | let _ = ((y - 1) + x) / y;
1414
| ^^^^^^^^^^^^^^^^^ help: consider using `.div_ceil()`: `x.div_ceil(y)`
1515

1616
error: manually reimplementing `div_ceil`
17-
--> tests/ui/manual_div_ceil.rs:13:13
17+
--> tests/ui/manual_div_ceil.rs:29:13
1818
|
1919
LL | let _ = (x + y - 1) / y;
2020
| ^^^^^^^^^^^^^^^ help: consider using `.div_ceil()`: `x.div_ceil(y)`
2121

2222
error: manually reimplementing `div_ceil`
23-
--> tests/ui/manual_div_ceil.rs:16:13
23+
--> tests/ui/manual_div_ceil.rs:32:13
2424
|
2525
LL | let _ = (7_u32 + (4 - 1)) / 4;
2626
| ^^^^^^^^^^^^^^^^^^^^^ help: consider using `.div_ceil()`: `7_u32.div_ceil(4)`
2727

2828
error: manually reimplementing `div_ceil`
29-
--> tests/ui/manual_div_ceil.rs:18:13
29+
--> tests/ui/manual_div_ceil.rs:34:13
3030
|
3131
LL | let _ = (7_i32 as u32 + (4 - 1)) / 4;
3232
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `.div_ceil()`: `(7_i32 as u32).div_ceil(4)`
3333

3434
error: manually reimplementing `div_ceil`
35-
--> tests/ui/manual_div_ceil.rs:39:13
35+
--> tests/ui/manual_div_ceil.rs:6:17
36+
|
37+
LL | let _ = (x + 7) / 8;
38+
| ^^^^^^^^^^^ help: consider using `.div_ceil()`: `x.div_ceil(8)`
39+
...
40+
LL | y!();
41+
| ---- in this macro invocation
42+
|
43+
= note: this error originates in the macro `y` (in Nightly builds, run with -Z macro-backtrace for more info)
44+
45+
error: manually reimplementing `div_ceil`
46+
--> tests/ui/manual_div_ceil.rs:8:17
47+
|
48+
LL | let _ = (7 + x) / 8;
49+
| ^^^^^^^^^^^ help: consider using `.div_ceil()`: `x.div_ceil(8)`
50+
...
51+
LL | y!();
52+
| ---- in this macro invocation
53+
|
54+
= note: this error originates in the macro `y` (in Nightly builds, run with -Z macro-backtrace for more info)
55+
56+
error: manually reimplementing `div_ceil`
57+
--> tests/ui/manual_div_ceil.rs:56:13
58+
|
59+
LL | let _ = (33u32 + 7) / eight!();
60+
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider using `.div_ceil()`: `33u32.div_ceil(eight!())`
61+
62+
error: manually reimplementing `div_ceil`
63+
--> tests/ui/manual_div_ceil.rs:62:13
3664
|
3765
LL | let _ = (2048 + x - 1) / x;
3866
| ^^^^^^^^^^^^^^^^^^ help: consider using `.div_ceil()`: `2048_usize.div_ceil(x)`
3967

4068
error: manually reimplementing `div_ceil`
41-
--> tests/ui/manual_div_ceil.rs:43:13
69+
--> tests/ui/manual_div_ceil.rs:66:13
4270
|
4371
LL | let _ = (2048usize + x - 1) / x;
4472
| ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `.div_ceil()`: `2048usize.div_ceil(x)`
4573

4674
error: manually reimplementing `div_ceil`
47-
--> tests/ui/manual_div_ceil.rs:47:13
75+
--> tests/ui/manual_div_ceil.rs:70:13
4876
|
4977
LL | let _ = (2048_usize + x - 1) / x;
5078
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `.div_ceil()`: `2048_usize.div_ceil(x)`
5179

5280
error: manually reimplementing `div_ceil`
53-
--> tests/ui/manual_div_ceil.rs:51:13
81+
--> tests/ui/manual_div_ceil.rs:74:13
5482
|
5583
LL | let _ = (x + 4 - 1) / 4;
5684
| ^^^^^^^^^^^^^^^ help: consider using `.div_ceil()`: `x.div_ceil(4)`
5785

5886
error: manually reimplementing `div_ceil`
59-
--> tests/ui/manual_div_ceil.rs:54:18
87+
--> tests/ui/manual_div_ceil.rs:77:18
6088
|
6189
LL | let _: u32 = (2048 + 6 - 1) / 6;
6290
| ^^^^^^^^^^^^^^^^^^ help: consider using `.div_ceil()`: `2048_u32.div_ceil(6)`
6391

6492
error: manually reimplementing `div_ceil`
65-
--> tests/ui/manual_div_ceil.rs:56:20
93+
--> tests/ui/manual_div_ceil.rs:79:20
6694
|
6795
LL | let _: usize = (2048 + 6 - 1) / 6;
6896
| ^^^^^^^^^^^^^^^^^^ help: consider using `.div_ceil()`: `2048_usize.div_ceil(6)`
6997

7098
error: manually reimplementing `div_ceil`
71-
--> tests/ui/manual_div_ceil.rs:58:18
99+
--> tests/ui/manual_div_ceil.rs:81:18
72100
|
73101
LL | let _: u32 = (0x2048 + 0x6 - 1) / 0x6;
74102
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `.div_ceil()`: `0x2048_u32.div_ceil(0x6)`
75103

76104
error: manually reimplementing `div_ceil`
77-
--> tests/ui/manual_div_ceil.rs:61:13
105+
--> tests/ui/manual_div_ceil.rs:84:13
78106
|
79107
LL | let _ = (2048 + 6u32 - 1) / 6u32;
80108
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `.div_ceil()`: `2048_u32.div_ceil(6u32)`
81109

82110
error: manually reimplementing `div_ceil`
83-
--> tests/ui/manual_div_ceil.rs:64:13
111+
--> tests/ui/manual_div_ceil.rs:87:13
84112
|
85113
LL | let _ = (1_000_000 + 6u32 - 1) / 6u32;
86114
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `.div_ceil()`: `1_000_000_u32.div_ceil(6u32)`
87115

88116
error: manually reimplementing `div_ceil`
89-
--> tests/ui/manual_div_ceil.rs:70:13
117+
--> tests/ui/manual_div_ceil.rs:93:13
90118
|
91119
LL | let _ = (x + 7) / 8;
92120
| ^^^^^^^^^^^ help: consider using `.div_ceil()`: `x.div_ceil(8)`
93121

94122
error: manually reimplementing `div_ceil`
95-
--> tests/ui/manual_div_ceil.rs:72:13
123+
--> tests/ui/manual_div_ceil.rs:95:13
96124
|
97125
LL | let _ = (7 + x) / 8;
98126
| ^^^^^^^^^^^ help: consider using `.div_ceil()`: `x.div_ceil(8)`
99127

100-
error: aborting due to 16 previous errors
128+
error: aborting due to 19 previous errors
101129

0 commit comments

Comments
 (0)