Skip to content

Commit 4da3bbd

Browse files
committed
update config and suggest
1 parent 10ec6d4 commit 4da3bbd

File tree

8 files changed

+63
-75
lines changed

8 files changed

+63
-75
lines changed

clippy_lints/src/indexing_slicing.rs

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
//! lint on indexing and slicing operations
22
33
use clippy_utils::consts::{constant, Constant};
4-
use clippy_utils::diagnostics::{span_lint, span_lint_and_help};
4+
use clippy_utils::diagnostics::{span_lint, span_lint_and_then};
55
use clippy_utils::higher;
66
use rustc_ast::ast::RangeLimits;
7+
use rustc_errors::Applicability;
78
use rustc_hir::{Expr, ExprKind};
89
use rustc_lint::{LateContext, LateLintPass};
910
use rustc_middle::ty;
@@ -86,18 +87,20 @@ impl_lint_pass!(IndexingSlicing => [INDEXING_SLICING, OUT_OF_BOUNDS_INDEXING]);
8687

8788
#[derive(Copy, Clone)]
8889
pub struct IndexingSlicing {
89-
suppress_lint_in_const: bool,
90+
suppress_restriction_lint_in_const: bool,
9091
}
9192

9293
impl IndexingSlicing {
93-
pub fn new(suppress_lint_in_const: bool) -> Self {
94-
Self { suppress_lint_in_const }
94+
pub fn new(suppress_restriction_lint_in_const: bool) -> Self {
95+
Self {
96+
suppress_restriction_lint_in_const,
97+
}
9598
}
9699
}
97100

98101
impl<'tcx> LateLintPass<'tcx> for IndexingSlicing {
99102
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
100-
if self.suppress_lint_in_const && cx.tcx.hir().is_inside_const_context(expr.hir_id) {
103+
if self.suppress_restriction_lint_in_const && cx.tcx.hir().is_inside_const_context(expr.hir_id) {
101104
return;
102105
}
103106

@@ -152,12 +155,19 @@ impl<'tcx> LateLintPass<'tcx> for IndexingSlicing {
152155
(None, None) => return, // [..] is ok.
153156
};
154157

155-
span_lint_and_help(cx, INDEXING_SLICING, expr.span, "slicing may panic", None, help_msg);
158+
span_lint_and_then(cx, INDEXING_SLICING, expr.span, "slicing may panic", |diag| {
159+
let note = if cx.tcx.hir().is_inside_const_context(expr.hir_id) {
160+
"the suggestion might not be applicable in constant blocks"
161+
} else {
162+
""
163+
};
164+
diag.span_suggestion(expr.span, help_msg, note, Applicability::MachineApplicable);
165+
});
156166
} else {
157167
// Catchall non-range index, i.e., [n] or [n << m]
158168
if let ty::Array(..) = ty.kind() {
159169
// Index is a const block.
160-
if self.suppress_lint_in_const && let ExprKind::ConstBlock(..) = index.kind {
170+
if self.suppress_restriction_lint_in_const && let ExprKind::ConstBlock(..) = index.kind {
161171
return;
162172
}
163173
// Index is a constant uint.
@@ -167,14 +177,19 @@ impl<'tcx> LateLintPass<'tcx> for IndexingSlicing {
167177
}
168178
}
169179

170-
span_lint_and_help(
171-
cx,
172-
INDEXING_SLICING,
173-
expr.span,
174-
"indexing may panic",
175-
None,
176-
"consider using `.get(n)` or `.get_mut(n)` instead",
177-
);
180+
span_lint_and_then(cx, INDEXING_SLICING, expr.span, "indexing may panic", |diag| {
181+
let note = if cx.tcx.hir().is_inside_const_context(expr.hir_id) {
182+
"the suggestion might not be applicable in constant blocks"
183+
} else {
184+
""
185+
};
186+
diag.span_suggestion(
187+
expr.span,
188+
"consider using `.get(n)` or `.get_mut(n)` instead",
189+
note,
190+
Applicability::MachineApplicable,
191+
);
192+
});
178193
}
179194
}
180195
}

clippy_lints/src/lib.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
599599
let avoid_breaking_exported_api = conf.avoid_breaking_exported_api;
600600
let allow_expect_in_tests = conf.allow_expect_in_tests;
601601
let allow_unwrap_in_tests = conf.allow_unwrap_in_tests;
602-
let suppress_lint_in_const = conf.suppress_lint_in_const;
602+
let suppress_restriction_lint_in_const = conf.suppress_restriction_lint_in_const;
603603
store.register_late_pass(move |_| Box::new(approx_const::ApproxConstant::new(msrv)));
604604
store.register_late_pass(move |_| {
605605
Box::new(methods::Methods::new(
@@ -721,7 +721,11 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
721721
store.register_late_pass(|_| Box::new(inherent_impl::MultipleInherentImpl));
722722
store.register_late_pass(|_| Box::new(neg_cmp_op_on_partial_ord::NoNegCompOpForPartialOrd));
723723
store.register_late_pass(|_| Box::new(unwrap::Unwrap));
724-
store.register_late_pass(move |_| Box::new(indexing_slicing::IndexingSlicing::new(suppress_lint_in_const)));
724+
store.register_late_pass(move |_| {
725+
Box::new(indexing_slicing::IndexingSlicing::new(
726+
suppress_restriction_lint_in_const,
727+
))
728+
});
725729
store.register_late_pass(|_| Box::new(non_copy_const::NonCopyConst));
726730
store.register_late_pass(|_| Box::new(ptr_offset_with_cast::PtrOffsetWithCast));
727731
store.register_late_pass(|_| Box::new(redundant_clone::RedundantClone));

clippy_lints/src/utils/conf.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,8 +404,12 @@ define_Conf! {
404404
(ignore_interior_mutability: Vec<String> = Vec::from(["bytes::Bytes".into()])),
405405
/// Lint: INDEXING_SLICING
406406
///
407-
/// Whether to suppress lint in const function
408-
(suppress_lint_in_const: bool = true),
407+
/// Whether to suppress a restriction lint in constant code. In same
408+
/// cases the restructured operation might not be unavoidable, as the
409+
/// suggested counterparts are unavailable in constant code. This
410+
/// configuration will cause restriction lints to trigger even
411+
/// if no suggestion can be made.
412+
(suppress_restriction_lint_in_const: bool = true),
409413
}
410414

411415
/// Search for the configuration file.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
suppress-lint-in-const = false
1+
suppress-restriction-lint-in-const = false

tests/ui-toml/suppress_lint_in_const/test.stderr

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ error: indexing may panic
22
--> $DIR/test.rs:11:9
33
|
44
LL | self.value[0] & 0b1000_0000 != 0
5-
| ^^^^^^^^^^^^^
5+
| ^^^^^^^^^^^^^ help: consider using `.get(n)` or `.get_mut(n)` instead: `the suggestion might not be applicable in constant blocks`
66
|
7-
= help: consider using `.get(n)` or `.get_mut(n)` instead
87
= note: `-D clippy::indexing-slicing` implied by `-D warnings`
98

109
error: aborting due to previous error

tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ error: error reading Clippy's configuration file `$DIR/clippy.toml`: unknown fie
3434
pass-by-value-size-limit
3535
single-char-binding-names-threshold
3636
standard-macro-braces
37-
suppress-lint-in-const
37+
suppress-restriction-lint-in-const
3838
third-party
3939
too-large-for-stack
4040
too-many-arguments-threshold

tests/ui/indexing_slicing_index.stderr

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,50 +14,39 @@ error: indexing may panic
1414
--> $DIR/indexing_slicing_index.rs:22:5
1515
|
1616
LL | x[index];
17-
| ^^^^^^^^
17+
| ^^^^^^^^ help: consider using `.get(n)` or `.get_mut(n)` instead
1818
|
19-
= help: consider using `.get(n)` or `.get_mut(n)` instead
2019
= note: `-D clippy::indexing-slicing` implied by `-D warnings`
2120

2221
error: indexing may panic
2322
--> $DIR/indexing_slicing_index.rs:38:5
2423
|
2524
LL | v[0];
26-
| ^^^^
27-
|
28-
= help: consider using `.get(n)` or `.get_mut(n)` instead
25+
| ^^^^ help: consider using `.get(n)` or `.get_mut(n)` instead
2926

3027
error: indexing may panic
3128
--> $DIR/indexing_slicing_index.rs:39:5
3229
|
3330
LL | v[10];
34-
| ^^^^^
35-
|
36-
= help: consider using `.get(n)` or `.get_mut(n)` instead
31+
| ^^^^^ help: consider using `.get(n)` or `.get_mut(n)` instead
3732

3833
error: indexing may panic
3934
--> $DIR/indexing_slicing_index.rs:40:5
4035
|
4136
LL | v[1 << 3];
42-
| ^^^^^^^^^
43-
|
44-
= help: consider using `.get(n)` or `.get_mut(n)` instead
37+
| ^^^^^^^^^ help: consider using `.get(n)` or `.get_mut(n)` instead
4538

4639
error: indexing may panic
4740
--> $DIR/indexing_slicing_index.rs:46:5
4841
|
4942
LL | v[N];
50-
| ^^^^
51-
|
52-
= help: consider using `.get(n)` or `.get_mut(n)` instead
43+
| ^^^^ help: consider using `.get(n)` or `.get_mut(n)` instead
5344

5445
error: indexing may panic
5546
--> $DIR/indexing_slicing_index.rs:47:5
5647
|
5748
LL | v[M];
58-
| ^^^^
59-
|
60-
= help: consider using `.get(n)` or `.get_mut(n)` instead
49+
| ^^^^ help: consider using `.get(n)` or `.get_mut(n)` instead
6150

6251
error[E0080]: evaluation of constant value failed
6352
--> $DIR/indexing_slicing_index.rs:10:24

tests/ui/indexing_slicing_slice.stderr

Lines changed: 12 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,50 +2,39 @@ error: slicing may panic
22
--> $DIR/indexing_slicing_slice.rs:12:6
33
|
44
LL | &x[index..];
5-
| ^^^^^^^^^^
5+
| ^^^^^^^^^^ help: consider using `.get(n..)` or .get_mut(n..)` instead
66
|
7-
= help: consider using `.get(n..)` or .get_mut(n..)` instead
87
= note: `-D clippy::indexing-slicing` implied by `-D warnings`
98

109
error: slicing may panic
1110
--> $DIR/indexing_slicing_slice.rs:13:6
1211
|
1312
LL | &x[..index];
14-
| ^^^^^^^^^^
15-
|
16-
= help: consider using `.get(..n)`or `.get_mut(..n)` instead
13+
| ^^^^^^^^^^ help: consider using `.get(..n)`or `.get_mut(..n)` instead
1714

1815
error: slicing may panic
1916
--> $DIR/indexing_slicing_slice.rs:14:6
2017
|
2118
LL | &x[index_from..index_to];
22-
| ^^^^^^^^^^^^^^^^^^^^^^^
23-
|
24-
= help: consider using `.get(n..m)` or `.get_mut(n..m)` instead
19+
| ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `.get(n..m)` or `.get_mut(n..m)` instead
2520

2621
error: slicing may panic
2722
--> $DIR/indexing_slicing_slice.rs:15:6
2823
|
2924
LL | &x[index_from..][..index_to]; // Two lint reports, one for [index_from..] and another for [..index_to].
30-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
31-
|
32-
= help: consider using `.get(..n)`or `.get_mut(..n)` instead
25+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `.get(..n)`or `.get_mut(..n)` instead
3326

3427
error: slicing may panic
3528
--> $DIR/indexing_slicing_slice.rs:15:6
3629
|
3730
LL | &x[index_from..][..index_to]; // Two lint reports, one for [index_from..] and another for [..index_to].
38-
| ^^^^^^^^^^^^^^^
39-
|
40-
= help: consider using `.get(n..)` or .get_mut(n..)` instead
31+
| ^^^^^^^^^^^^^^^ help: consider using `.get(n..)` or .get_mut(n..)` instead
4132

4233
error: slicing may panic
4334
--> $DIR/indexing_slicing_slice.rs:16:6
4435
|
4536
LL | &x[5..][..10]; // Two lint reports, one for out of bounds [5..] and another for slicing [..10].
46-
| ^^^^^^^^^^^^
47-
|
48-
= help: consider using `.get(..n)`or `.get_mut(..n)` instead
37+
| ^^^^^^^^^^^^ help: consider using `.get(..n)`or `.get_mut(..n)` instead
4938

5039
error: range is out of bounds
5140
--> $DIR/indexing_slicing_slice.rs:16:8
@@ -59,17 +48,13 @@ error: slicing may panic
5948
--> $DIR/indexing_slicing_slice.rs:17:6
6049
|
6150
LL | &x[0..][..3];
62-
| ^^^^^^^^^^^
63-
|
64-
= help: consider using `.get(..n)`or `.get_mut(..n)` instead
51+
| ^^^^^^^^^^^ help: consider using `.get(..n)`or `.get_mut(..n)` instead
6552

6653
error: slicing may panic
6754
--> $DIR/indexing_slicing_slice.rs:18:6
6855
|
6956
LL | &x[1..][..5];
70-
| ^^^^^^^^^^^
71-
|
72-
= help: consider using `.get(..n)`or `.get_mut(..n)` instead
57+
| ^^^^^^^^^^^ help: consider using `.get(..n)`or `.get_mut(..n)` instead
7358

7459
error: range is out of bounds
7560
--> $DIR/indexing_slicing_slice.rs:25:12
@@ -87,17 +72,13 @@ error: slicing may panic
8772
--> $DIR/indexing_slicing_slice.rs:31:6
8873
|
8974
LL | &v[10..100];
90-
| ^^^^^^^^^^
91-
|
92-
= help: consider using `.get(n..m)` or `.get_mut(n..m)` instead
75+
| ^^^^^^^^^^ help: consider using `.get(n..m)` or `.get_mut(n..m)` instead
9376

9477
error: slicing may panic
9578
--> $DIR/indexing_slicing_slice.rs:32:6
9679
|
9780
LL | &x[10..][..100]; // Two lint reports, one for [10..] and another for [..100].
98-
| ^^^^^^^^^^^^^^
99-
|
100-
= help: consider using `.get(..n)`or `.get_mut(..n)` instead
81+
| ^^^^^^^^^^^^^^ help: consider using `.get(..n)`or `.get_mut(..n)` instead
10182

10283
error: range is out of bounds
10384
--> $DIR/indexing_slicing_slice.rs:32:8
@@ -109,17 +90,13 @@ error: slicing may panic
10990
--> $DIR/indexing_slicing_slice.rs:33:6
11091
|
11192
LL | &v[10..];
112-
| ^^^^^^^
113-
|
114-
= help: consider using `.get(n..)` or .get_mut(n..)` instead
93+
| ^^^^^^^ help: consider using `.get(n..)` or .get_mut(n..)` instead
11594

11695
error: slicing may panic
11796
--> $DIR/indexing_slicing_slice.rs:34:6
11897
|
11998
LL | &v[..100];
120-
| ^^^^^^^^
121-
|
122-
= help: consider using `.get(..n)`or `.get_mut(..n)` instead
99+
| ^^^^^^^^ help: consider using `.get(..n)`or `.get_mut(..n)` instead
123100

124101
error: aborting due to 16 previous errors
125102

0 commit comments

Comments
 (0)