Skip to content

Commit 67a9413

Browse files
committed
change note style
1 parent 1fc98c5 commit 67a9413

File tree

4 files changed

+120
-39
lines changed

4 files changed

+120
-39
lines changed

clippy_lints/src/indexing_slicing.rs

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use clippy_utils::consts::{constant, Constant};
44
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;
87
use rustc_hir::{Expr, ExprKind};
98
use rustc_lint::{LateContext, LateLintPass};
109
use rustc_middle::ty;
@@ -105,6 +104,7 @@ impl<'tcx> LateLintPass<'tcx> for IndexingSlicing {
105104
}
106105

107106
if let ExprKind::Index(array, index) = &expr.kind {
107+
let note = "the suggestion might not be applicable in constant blocks";
108108
let ty = cx.typeck_results().expr_ty(array).peel_refs();
109109
if let Some(range) = higher::Range::hir(index) {
110110
// Ranged indexes, i.e., &x[n..m], &x[n..], &x[..n] and &x[..]
@@ -156,12 +156,11 @@ impl<'tcx> LateLintPass<'tcx> for IndexingSlicing {
156156
};
157157

158158
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);
159+
diag.help(help_msg);
160+
161+
if cx.tcx.hir().is_inside_const_context(expr.hir_id) {
162+
diag.note(note);
163+
}
165164
});
166165
} else {
167166
// Catchall non-range index, i.e., [n] or [n << m]
@@ -178,17 +177,11 @@ impl<'tcx> LateLintPass<'tcx> for IndexingSlicing {
178177
}
179178

180179
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-
);
180+
diag.help("consider using `.get(n)` or `.get_mut(n)` instead");
181+
182+
if cx.tcx.hir().is_inside_const_context(expr.hir_id) {
183+
diag.note(note);
184+
}
192185
});
193186
}
194187
}

tests/ui-toml/suppress_lint_in_const/test.stderr

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

911
error: aborting due to previous error

tests/ui/indexing_slicing_index.stderr

Lines changed: 71 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
error: indexing may panic
2+
--> $DIR/indexing_slicing_index.rs:9:20
3+
|
4+
LL | const REF: &i32 = &ARR[idx()]; // Ok, should not produce stderr.
5+
| ^^^^^^^^^^
6+
|
7+
= help: consider using `.get(n)` or `.get_mut(n)` instead
8+
= note: the suggestion might not be applicable in constant blocks
9+
= note: `-D clippy::indexing-slicing` implied by `-D warnings`
10+
11+
error: indexing may panic
12+
--> $DIR/indexing_slicing_index.rs:10:24
13+
|
14+
LL | const REF_ERR: &i32 = &ARR[idx4()]; // Ok, let rustc handle const contexts.
15+
| ^^^^^^^^^^^
16+
|
17+
= help: consider using `.get(n)` or `.get_mut(n)` instead
18+
= note: the suggestion might not be applicable in constant blocks
19+
120
error[E0080]: evaluation of `main::{constant#3}` failed
221
--> $DIR/indexing_slicing_index.rs:31:14
322
|
@@ -14,46 +33,90 @@ error: indexing may panic
1433
--> $DIR/indexing_slicing_index.rs:22:5
1534
|
1635
LL | x[index];
17-
| ^^^^^^^^ help: consider using `.get(n)` or `.get_mut(n)` instead
36+
| ^^^^^^^^
1837
|
19-
= note: `-D clippy::indexing-slicing` implied by `-D warnings`
38+
= help: consider using `.get(n)` or `.get_mut(n)` instead
39+
40+
error: indexing may panic
41+
--> $DIR/indexing_slicing_index.rs:28:5
42+
|
43+
LL | x[const { idx() }]; // Ok, should not produce stderr.
44+
| ^^^^^^^^^^^^^^^^^^
45+
|
46+
= help: consider using `.get(n)` or `.get_mut(n)` instead
47+
48+
error: indexing may panic
49+
--> $DIR/indexing_slicing_index.rs:29:5
50+
|
51+
LL | x[const { idx4() }]; // Ok, let rustc's `unconditional_panic` lint handle `usize` indexing on arrays.
52+
| ^^^^^^^^^^^^^^^^^^^
53+
|
54+
= help: consider using `.get(n)` or `.get_mut(n)` instead
55+
56+
error: indexing may panic
57+
--> $DIR/indexing_slicing_index.rs:30:14
58+
|
59+
LL | const { &ARR[idx()] }; // Ok, should not produce stderr.
60+
| ^^^^^^^^^^
61+
|
62+
= help: consider using `.get(n)` or `.get_mut(n)` instead
63+
= note: the suggestion might not be applicable in constant blocks
64+
65+
error: indexing may panic
66+
--> $DIR/indexing_slicing_index.rs:31:14
67+
|
68+
LL | const { &ARR[idx4()] }; // Ok, let rustc handle const contexts.
69+
| ^^^^^^^^^^^
70+
|
71+
= help: consider using `.get(n)` or `.get_mut(n)` instead
72+
= note: the suggestion might not be applicable in constant blocks
2073

2174
error: indexing may panic
2275
--> $DIR/indexing_slicing_index.rs:38:5
2376
|
2477
LL | v[0];
25-
| ^^^^ help: consider using `.get(n)` or `.get_mut(n)` instead
78+
| ^^^^
79+
|
80+
= help: consider using `.get(n)` or `.get_mut(n)` instead
2681

2782
error: indexing may panic
2883
--> $DIR/indexing_slicing_index.rs:39:5
2984
|
3085
LL | v[10];
31-
| ^^^^^ help: consider using `.get(n)` or `.get_mut(n)` instead
86+
| ^^^^^
87+
|
88+
= help: consider using `.get(n)` or `.get_mut(n)` instead
3289

3390
error: indexing may panic
3491
--> $DIR/indexing_slicing_index.rs:40:5
3592
|
3693
LL | v[1 << 3];
37-
| ^^^^^^^^^ help: consider using `.get(n)` or `.get_mut(n)` instead
94+
| ^^^^^^^^^
95+
|
96+
= help: consider using `.get(n)` or `.get_mut(n)` instead
3897

3998
error: indexing may panic
4099
--> $DIR/indexing_slicing_index.rs:46:5
41100
|
42101
LL | v[N];
43-
| ^^^^ help: consider using `.get(n)` or `.get_mut(n)` instead
102+
| ^^^^
103+
|
104+
= help: consider using `.get(n)` or `.get_mut(n)` instead
44105

45106
error: indexing may panic
46107
--> $DIR/indexing_slicing_index.rs:47:5
47108
|
48109
LL | v[M];
49-
| ^^^^ help: consider using `.get(n)` or `.get_mut(n)` instead
110+
| ^^^^
111+
|
112+
= help: consider using `.get(n)` or `.get_mut(n)` instead
50113

51114
error[E0080]: evaluation of constant value failed
52115
--> $DIR/indexing_slicing_index.rs:10:24
53116
|
54117
LL | const REF_ERR: &i32 = &ARR[idx4()]; // Ok, let rustc handle const contexts.
55118
| ^^^^^^^^^^^ index out of bounds: the length is 2 but the index is 4
56119

57-
error: aborting due to 8 previous errors
120+
error: aborting due to 14 previous errors
58121

59122
For more information about this error, try `rustc --explain E0080`.

tests/ui/indexing_slicing_slice.stderr

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

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

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

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

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

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

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

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

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

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

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

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

101124
error: aborting due to 16 previous errors
102125

0 commit comments

Comments
 (0)