Skip to content

Commit 610acda

Browse files
committed
Added suggestions and changes from review
Changed example suggestion from const to snippet Shortend error message Followed clippy naming conventions
1 parent d332ec5 commit 610acda

File tree

5 files changed

+28
-27
lines changed

5 files changed

+28
-27
lines changed

clippy_lints/src/declared_lints.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ pub static LINTS: &[&::declare_clippy_lint::LintInfo] = &[
352352
crate::methods::CASE_SENSITIVE_FILE_EXTENSION_COMPARISONS_INFO,
353353
crate::methods::CHARS_LAST_CMP_INFO,
354354
crate::methods::CHARS_NEXT_CMP_INFO,
355-
crate::methods::CHUNKS_EXACT_TO_AS_CHUNKS_INFO,
355+
crate::methods::CHUNKS_EXACT_WITH_CONST_SIZE_INFO,
356356
crate::methods::CLEAR_WITH_DRAIN_INFO,
357357
crate::methods::CLONED_INSTEAD_OF_COPIED_INFO,
358358
crate::methods::CLONE_ON_COPY_INFO,

clippy_lints/src/methods/chunks_exact_to_as_chunks.rs renamed to clippy_lints/src/methods/chunks_exact_with_const_size.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
use clippy_utils::consts::{ConstEvalCtxt, Constant};
22
use clippy_utils::diagnostics::span_lint_and_help;
33
use clippy_utils::msrvs::{self, Msrv};
4+
use clippy_utils::source::snippet;
45
use clippy_utils::sym;
56
use rustc_hir::Expr;
67
use rustc_lint::LateContext;
78
use rustc_span::Symbol;
89

9-
use super::CHUNKS_EXACT_TO_AS_CHUNKS;
10+
use super::CHUNKS_EXACT_WITH_CONST_SIZE;
1011

1112
pub(super) fn check(
1213
cx: &LateContext<'_>,
@@ -29,21 +30,21 @@ pub(super) fn check(
2930

3031
// Check if argument is a constant
3132
let constant_eval = ConstEvalCtxt::new(cx);
32-
if let Some(Constant::Int(chunk_size)) = constant_eval.eval(arg) {
33+
if let Some(Constant::Int(_)) = constant_eval.eval(arg) {
3334
// Emit the lint
3435
let suggestion = if method_name == sym::chunks_exact_mut {
3536
"as_chunks_mut"
3637
} else {
3738
"as_chunks"
3839
};
39-
40+
let arg_str = snippet(cx, arg.span, "_");
4041
span_lint_and_help(
4142
cx,
42-
CHUNKS_EXACT_TO_AS_CHUNKS,
43+
CHUNKS_EXACT_WITH_CONST_SIZE,
4344
expr.span,
44-
format!("using `{method_name}` with a constant chunk size"),
45+
"chunks_exact_with_const_size",
4546
None,
46-
format!("consider using `{suggestion}::<{chunk_size}>()` for better ergonomics"),
47+
format!("consider using `{suggestion}::<{arg_str}>()` for better ergonomics"),
4748
);
4849
}
4950
}

clippy_lints/src/methods/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ mod chars_last_cmp;
99
mod chars_last_cmp_with_unwrap;
1010
mod chars_next_cmp;
1111
mod chars_next_cmp_with_unwrap;
12-
mod chunks_exact_to_as_chunks;
12+
mod chunks_exact_with_const_size;
1313
mod clear_with_drain;
1414
mod clone_on_copy;
1515
mod clone_on_ref_ptr;
@@ -2109,7 +2109,7 @@ declare_clippy_lint! {
21092109
/// for chunk in chunks {}
21102110
/// ```
21112111
#[clippy::version = "1.93.0"]
2112-
pub CHUNKS_EXACT_TO_AS_CHUNKS,
2112+
pub CHUNKS_EXACT_WITH_CONST_SIZE,
21132113
style,
21142114
"using `chunks_exact` with constant when `as_chunks` is more ergonomic"
21152115
}
@@ -4814,7 +4814,7 @@ impl_lint_pass!(Methods => [
48144814
ITER_NTH,
48154815
ITER_NTH_ZERO,
48164816
BYTES_NTH,
4817-
CHUNKS_EXACT_TO_AS_CHUNKS,
4817+
CHUNKS_EXACT_WITH_CONST_SIZE,
48184818
ITER_SKIP_NEXT,
48194819
GET_UNWRAP,
48204820
GET_LAST_WITH_LEN,
@@ -5744,7 +5744,7 @@ impl Methods {
57445744
);
57455745
},
57465746
(name @ (sym::chunks_exact | sym::chunks_exact_mut), [arg]) => {
5747-
chunks_exact_to_as_chunks::check(cx, expr, recv, arg, name, self.msrv);
5747+
chunks_exact_with_const_size::check(cx, expr, recv, arg, name, self.msrv);
57485748
},
57495749
_ => {},
57505750
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
#![warn(clippy::chunks_exact_to_as_chunks)]
1+
#![warn(clippy::chunks_exact_with_const_size)]
22
#![allow(unused)]
33

44
fn main() {
55
let slice = [1, 2, 3, 4, 5, 6, 7, 8];
66

77
// Should trigger lint - literal constant
88
let mut it = slice.chunks_exact(4);
9-
//~^ ERROR: using `chunks_exact` with a constant chunk size
9+
//~^ ERROR: chunks_exact_with_const_size
1010
for chunk in it {}
1111

1212
// Should trigger lint - const value
1313
const CHUNK_SIZE: usize = 4;
1414
let mut it = slice.chunks_exact(CHUNK_SIZE);
15-
//~^ ERROR: using `chunks_exact` with a constant chunk size
15+
//~^ ERROR: chunks_exact_with_const_size
1616
for chunk in it {}
1717

1818
// Should NOT trigger - runtime value
@@ -22,13 +22,13 @@ fn main() {
2222

2323
// Should trigger lint - with remainder
2424
let mut it = slice.chunks_exact(3);
25-
//~^ ERROR: using `chunks_exact` with a constant chunk size
25+
//~^ ERROR: chunks_exact_with_const_size
2626
for chunk in &mut it {}
2727
for e in it.remainder() {}
2828

2929
// Should trigger - mutable variant
3030
let mut arr = [1, 2, 3, 4, 5, 6, 7, 8];
3131
let mut it = arr.chunks_exact_mut(4);
32-
//~^ ERROR: using `chunks_exact_mut` with a constant chunk size
32+
//~^ ERROR: chunks_exact_with_const_size
3333
for chunk in it {}
3434
}

tests/ui/chunks_exact_to_as_chunks.stderr renamed to tests/ui/chunks_exact_with_const_size.stderr

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
1-
error: using `chunks_exact` with a constant chunk size
2-
--> tests/ui/chunks_exact_to_as_chunks.rs:8:18
1+
error: chunks_exact_with_const_size
2+
--> tests/ui/chunks_exact_with_const_size.rs:8:18
33
|
44
LL | let mut it = slice.chunks_exact(4);
55
| ^^^^^^^^^^^^^^^^^^^^^
66
|
77
= help: consider using `as_chunks::<4>()` for better ergonomics
8-
= note: `-D clippy::chunks-exact-to-as-chunks` implied by `-D warnings`
9-
= help: to override `-D warnings` add `#[allow(clippy::chunks_exact_to_as_chunks)]`
8+
= note: `-D clippy::chunks-exact-with-const-size` implied by `-D warnings`
9+
= help: to override `-D warnings` add `#[allow(clippy::chunks_exact_with_const_size)]`
1010

11-
error: using `chunks_exact` with a constant chunk size
12-
--> tests/ui/chunks_exact_to_as_chunks.rs:14:18
11+
error: chunks_exact_with_const_size
12+
--> tests/ui/chunks_exact_with_const_size.rs:14:18
1313
|
1414
LL | let mut it = slice.chunks_exact(CHUNK_SIZE);
1515
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1616
|
17-
= help: consider using `as_chunks::<4>()` for better ergonomics
17+
= help: consider using `as_chunks::<CHUNK_SIZE>()` for better ergonomics
1818

19-
error: using `chunks_exact` with a constant chunk size
20-
--> tests/ui/chunks_exact_to_as_chunks.rs:24:18
19+
error: chunks_exact_with_const_size
20+
--> tests/ui/chunks_exact_with_const_size.rs:24:18
2121
|
2222
LL | let mut it = slice.chunks_exact(3);
2323
| ^^^^^^^^^^^^^^^^^^^^^
2424
|
2525
= help: consider using `as_chunks::<3>()` for better ergonomics
2626

27-
error: using `chunks_exact_mut` with a constant chunk size
28-
--> tests/ui/chunks_exact_to_as_chunks.rs:31:18
27+
error: chunks_exact_with_const_size
28+
--> tests/ui/chunks_exact_with_const_size.rs:31:18
2929
|
3030
LL | let mut it = arr.chunks_exact_mut(4);
3131
| ^^^^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)