Skip to content

Commit 54a84d5

Browse files
committed
use it in some lints
1 parent 2b98b7b commit 54a84d5

11 files changed

+82
-21
lines changed

clippy_lints/src/almost_complete_range.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use clippy_config::Conf;
22
use clippy_utils::diagnostics::span_lint_and_then;
3+
use clippy_utils::is_from_proc_macro;
34
use clippy_utils::msrvs::{self, MsrvStack};
45
use clippy_utils::source::{trim_span, walk_span_to_context};
56
use rustc_ast::ast::{Expr, ExprKind, LitKind, Pat, PatKind, RangeEnd, RangeLimits};
@@ -45,6 +46,7 @@ impl EarlyLintPass for AlmostCompleteRange {
4546
if let ExprKind::Range(Some(start), Some(end), RangeLimits::HalfOpen) = &e.kind
4647
&& is_incomplete_range(start, end)
4748
&& !e.span.in_external_macro(cx.sess().source_map())
49+
&& !is_from_proc_macro(cx, e)
4850
{
4951
span_lint_and_then(
5052
cx,

clippy_lints/src/byte_char_slices.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2+
use clippy_utils::is_from_proc_macro;
23
use rustc_ast::ast::{BorrowKind, Expr, ExprKind, Mutability};
34
use rustc_ast::token::{Lit, LitKind};
45
use rustc_errors::Applicability;
@@ -33,6 +34,7 @@ impl EarlyLintPass for ByteCharSlice {
3334
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
3435
if let Some(slice) = is_byte_char_slices(expr)
3536
&& !expr.span.from_expansion()
37+
&& !is_from_proc_macro(cx, expr)
3638
{
3739
span_lint_and_sugg(
3840
cx,

clippy_lints/src/else_if_without_else.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
2+
use clippy_utils::is_from_proc_macro;
23
use rustc_ast::ast::{Expr, ExprKind};
34
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
45
use rustc_session::declare_lint_pass;
@@ -50,6 +51,7 @@ impl EarlyLintPass for ElseIfWithoutElse {
5051
if let ExprKind::If(_, _, Some(ref els)) = item.kind
5152
&& let ExprKind::If(_, _, None) = els.kind
5253
&& !item.span.in_external_macro(cx.sess().source_map())
54+
&& !is_from_proc_macro(cx, item)
5355
{
5456
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
5557
span_lint_and_then(

tests/ui/almost_complete_range.fixed

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#![allow(clippy::double_parens)]
99

1010
extern crate proc_macros;
11-
use proc_macros::{external, inline_macros};
11+
use proc_macros::{external, inline_macros, with_span};
1212

1313
#[inline_macros]
1414
fn main() {
@@ -83,6 +83,11 @@ fn main() {
8383
let _ = '0'..='9';
8484
//~^ almost_complete_range
8585
);
86+
with_span!(span
87+
let _ = 'a'..'z';
88+
let _ = 'A'..'Z';
89+
let _ = '0'..'9';
90+
);
8691
}
8792

8893
#[clippy::msrv = "1.25"]

tests/ui/almost_complete_range.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#![allow(clippy::double_parens)]
99

1010
extern crate proc_macros;
11-
use proc_macros::{external, inline_macros};
11+
use proc_macros::{external, inline_macros, with_span};
1212

1313
#[inline_macros]
1414
fn main() {
@@ -83,6 +83,14 @@ fn main() {
8383
let _ = '0'..'9';
8484
//~^ almost_complete_range
8585
);
86+
with_span!(span
87+
let _ = 'a'..'z';
88+
let _ = 'A'..'Z';
89+
let _ = '0'..'9';
90+
let _ = ('a')..'z';
91+
let _ = 'A'..('Z');
92+
let _ = ('0') .. ('9');
93+
);
8694
}
8795

8896
#[clippy::msrv = "1.25"]

tests/ui/almost_complete_range.stderr

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -152,71 +152,71 @@ LL | let _ = '0'..'9';
152152
= note: this error originates in the macro `__inline_mac_fn_main` (in Nightly builds, run with -Z macro-backtrace for more info)
153153

154154
error: almost complete ascii range
155-
--> tests/ui/almost_complete_range.rs:91:9
155+
--> tests/ui/almost_complete_range.rs:96:9
156156
|
157157
LL | 'a'..'z' => 1,
158158
| ^^^--^^^
159159
| |
160160
| help: use an inclusive range: `...`
161161

162162
error: almost complete ascii range
163-
--> tests/ui/almost_complete_range.rs:93:9
163+
--> tests/ui/almost_complete_range.rs:98:9
164164
|
165165
LL | 'A'..'Z' => 2,
166166
| ^^^--^^^
167167
| |
168168
| help: use an inclusive range: `...`
169169

170170
error: almost complete ascii range
171-
--> tests/ui/almost_complete_range.rs:95:9
171+
--> tests/ui/almost_complete_range.rs:100:9
172172
|
173173
LL | '0'..'9' => 3,
174174
| ^^^--^^^
175175
| |
176176
| help: use an inclusive range: `...`
177177

178178
error: almost complete ascii range
179-
--> tests/ui/almost_complete_range.rs:103:13
179+
--> tests/ui/almost_complete_range.rs:108:13
180180
|
181181
LL | let _ = 'a'..'z';
182182
| ^^^--^^^
183183
| |
184184
| help: use an inclusive range: `..=`
185185

186186
error: almost complete ascii range
187-
--> tests/ui/almost_complete_range.rs:105:13
187+
--> tests/ui/almost_complete_range.rs:110:13
188188
|
189189
LL | let _ = 'A'..'Z';
190190
| ^^^--^^^
191191
| |
192192
| help: use an inclusive range: `..=`
193193

194194
error: almost complete ascii range
195-
--> tests/ui/almost_complete_range.rs:107:13
195+
--> tests/ui/almost_complete_range.rs:112:13
196196
|
197197
LL | let _ = '0'..'9';
198198
| ^^^--^^^
199199
| |
200200
| help: use an inclusive range: `..=`
201201

202202
error: almost complete ascii range
203-
--> tests/ui/almost_complete_range.rs:110:9
203+
--> tests/ui/almost_complete_range.rs:115:9
204204
|
205205
LL | 'a'..'z' => 1,
206206
| ^^^--^^^
207207
| |
208208
| help: use an inclusive range: `..=`
209209

210210
error: almost complete ascii range
211-
--> tests/ui/almost_complete_range.rs:112:9
211+
--> tests/ui/almost_complete_range.rs:117:9
212212
|
213213
LL | 'A'..'Z' => 1,
214214
| ^^^--^^^
215215
| |
216216
| help: use an inclusive range: `..=`
217217

218218
error: almost complete ascii range
219-
--> tests/ui/almost_complete_range.rs:114:9
219+
--> tests/ui/almost_complete_range.rs:119:9
220220
|
221221
LL | '0'..'9' => 3,
222222
| ^^^--^^^

tests/ui/byte_char_slices.fixed

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
//@aux-build:proc_macros.rs
12
#![allow(unused)]
23
#![warn(clippy::byte_char_slices)]
34

5+
use proc_macros::with_span;
6+
47
fn main() {
58
let bad = b"abc";
69
//~^ byte_char_slices
@@ -15,4 +18,6 @@ fn main() {
1518
let good = [b'a', b'a'];
1619
//~^ useless_vec
1720
let good: u8 = [b'a', b'c'].into_iter().sum();
21+
22+
let bad = with_span!(span & [b'a', b'b', b'c']);
1823
}

tests/ui/byte_char_slices.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
//@aux-build:proc_macros.rs
12
#![allow(unused)]
23
#![warn(clippy::byte_char_slices)]
34

5+
use proc_macros::with_span;
6+
47
fn main() {
58
let bad = &[b'a', b'b', b'c'];
69
//~^ byte_char_slices
@@ -15,4 +18,6 @@ fn main() {
1518
let good = vec![b'a', b'a'];
1619
//~^ useless_vec
1720
let good: u8 = [b'a', b'c'].into_iter().sum();
21+
22+
let bad = with_span!(span & [b'a', b'b', b'c']);
1823
}

tests/ui/byte_char_slices.stderr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: can be more succinctly written as a byte str
2-
--> tests/ui/byte_char_slices.rs:5:15
2+
--> tests/ui/byte_char_slices.rs:8:15
33
|
44
LL | let bad = &[b'a', b'b', b'c'];
55
| ^^^^^^^^^^^^^^^^^^^ help: try: `b"abc"`
@@ -8,25 +8,25 @@ LL | let bad = &[b'a', b'b', b'c'];
88
= help: to override `-D warnings` add `#[allow(clippy::byte_char_slices)]`
99

1010
error: can be more succinctly written as a byte str
11-
--> tests/ui/byte_char_slices.rs:7:18
11+
--> tests/ui/byte_char_slices.rs:10:18
1212
|
1313
LL | let quotes = &[b'"', b'H', b'i'];
1414
| ^^^^^^^^^^^^^^^^^^^ help: try: `b"\"Hi"`
1515

1616
error: can be more succinctly written as a byte str
17-
--> tests/ui/byte_char_slices.rs:9:18
17+
--> tests/ui/byte_char_slices.rs:12:18
1818
|
1919
LL | let quotes = &[b'\'', b'S', b'u', b'p'];
2020
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `b"'Sup"`
2121

2222
error: can be more succinctly written as a byte str
23-
--> tests/ui/byte_char_slices.rs:11:19
23+
--> tests/ui/byte_char_slices.rs:14:19
2424
|
2525
LL | let escapes = &[b'\x42', b'E', b's', b'c'];
2626
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `b"\x42Esc"`
2727

2828
error: useless use of `vec!`
29-
--> tests/ui/byte_char_slices.rs:15:16
29+
--> tests/ui/byte_char_slices.rs:18:16
3030
|
3131
LL | let good = vec![b'a', b'a'];
3232
| ^^^^^^^^^^^^^^^^ help: you can use an array directly: `[b'a', b'a']`

tests/ui/else_if_without_else.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
//@aux-build:proc_macros.rs
12
#![warn(clippy::else_if_without_else)]
23
#![allow(clippy::collapsible_else_if)]
34

5+
use proc_macros::{external, inline_macros, with_span};
6+
47
fn bla1() -> bool {
58
unimplemented!()
69
}
@@ -17,6 +20,7 @@ fn bla5() -> bool {
1720
unimplemented!()
1821
}
1922

23+
#[inline_macros]
2024
fn main() {
2125
if bla1() {
2226
println!("if");
@@ -123,4 +127,20 @@ fn main() {
123127
println!("else if 4");
124128
}
125129
}
130+
131+
inline!(if bla1() {
132+
println!("if");
133+
} else if bla2() {
134+
//~^ else_if_without_else
135+
});
136+
137+
external!(if bla1() {
138+
println!("if");
139+
} else if bla2() {
140+
});
141+
142+
with_span!(span; if bla1() {
143+
println!("if");
144+
} else if bla2() {
145+
});
126146
}

0 commit comments

Comments
 (0)