change note style

This commit is contained in:
naosense 2022-11-24 09:43:11 +08:00
parent 1fc98c51df
commit 67a94135cb
4 changed files with 120 additions and 39 deletions

View File

@ -4,7 +4,6 @@
use clippy_utils::diagnostics::{span_lint, span_lint_and_then};
use clippy_utils::higher;
use rustc_ast::ast::RangeLimits;
use rustc_errors::Applicability;
use rustc_hir::{Expr, ExprKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::ty;
@ -105,6 +104,7 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
}
if let ExprKind::Index(array, index) = &expr.kind {
let note = "the suggestion might not be applicable in constant blocks";
let ty = cx.typeck_results().expr_ty(array).peel_refs();
if let Some(range) = higher::Range::hir(index) {
// Ranged indexes, i.e., &x[n..m], &x[n..], &x[..n] and &x[..]
@ -156,12 +156,11 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
};
span_lint_and_then(cx, INDEXING_SLICING, expr.span, "slicing may panic", |diag| {
let note = if cx.tcx.hir().is_inside_const_context(expr.hir_id) {
"the suggestion might not be applicable in constant blocks"
} else {
""
};
diag.span_suggestion(expr.span, help_msg, note, Applicability::MachineApplicable);
diag.help(help_msg);
if cx.tcx.hir().is_inside_const_context(expr.hir_id) {
diag.note(note);
}
});
} else {
// Catchall non-range index, i.e., [n] or [n << m]
@ -178,17 +177,11 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
}
span_lint_and_then(cx, INDEXING_SLICING, expr.span, "indexing may panic", |diag| {
let note = if cx.tcx.hir().is_inside_const_context(expr.hir_id) {
"the suggestion might not be applicable in constant blocks"
} else {
""
};
diag.span_suggestion(
expr.span,
"consider using `.get(n)` or `.get_mut(n)` instead",
note,
Applicability::MachineApplicable,
);
diag.help("consider using `.get(n)` or `.get_mut(n)` instead");
if cx.tcx.hir().is_inside_const_context(expr.hir_id) {
diag.note(note);
}
});
}
}

View File

@ -2,8 +2,10 @@ error: indexing may panic
--> $DIR/test.rs:11:9
|
LL | self.value[0] & 0b1000_0000 != 0
| ^^^^^^^^^^^^^ help: consider using `.get(n)` or `.get_mut(n)` instead: `the suggestion might not be applicable in constant blocks`
| ^^^^^^^^^^^^^
|
= help: consider using `.get(n)` or `.get_mut(n)` instead
= note: the suggestion might not be applicable in constant blocks
= note: `-D clippy::indexing-slicing` implied by `-D warnings`
error: aborting due to previous error

View File

@ -1,3 +1,22 @@
error: indexing may panic
--> $DIR/indexing_slicing_index.rs:9:20
|
LL | const REF: &i32 = &ARR[idx()]; // Ok, should not produce stderr.
| ^^^^^^^^^^
|
= help: consider using `.get(n)` or `.get_mut(n)` instead
= note: the suggestion might not be applicable in constant blocks
= note: `-D clippy::indexing-slicing` implied by `-D warnings`
error: indexing may panic
--> $DIR/indexing_slicing_index.rs:10:24
|
LL | const REF_ERR: &i32 = &ARR[idx4()]; // Ok, let rustc handle const contexts.
| ^^^^^^^^^^^
|
= help: consider using `.get(n)` or `.get_mut(n)` instead
= note: the suggestion might not be applicable in constant blocks
error[E0080]: evaluation of `main::{constant#3}` failed
--> $DIR/indexing_slicing_index.rs:31:14
|
@ -14,39 +33,83 @@ error: indexing may panic
--> $DIR/indexing_slicing_index.rs:22:5
|
LL | x[index];
| ^^^^^^^^ help: consider using `.get(n)` or `.get_mut(n)` instead
| ^^^^^^^^
|
= note: `-D clippy::indexing-slicing` implied by `-D warnings`
= help: consider using `.get(n)` or `.get_mut(n)` instead
error: indexing may panic
--> $DIR/indexing_slicing_index.rs:28:5
|
LL | x[const { idx() }]; // Ok, should not produce stderr.
| ^^^^^^^^^^^^^^^^^^
|
= help: consider using `.get(n)` or `.get_mut(n)` instead
error: indexing may panic
--> $DIR/indexing_slicing_index.rs:29:5
|
LL | x[const { idx4() }]; // Ok, let rustc's `unconditional_panic` lint handle `usize` indexing on arrays.
| ^^^^^^^^^^^^^^^^^^^
|
= help: consider using `.get(n)` or `.get_mut(n)` instead
error: indexing may panic
--> $DIR/indexing_slicing_index.rs:30:14
|
LL | const { &ARR[idx()] }; // Ok, should not produce stderr.
| ^^^^^^^^^^
|
= help: consider using `.get(n)` or `.get_mut(n)` instead
= note: the suggestion might not be applicable in constant blocks
error: indexing may panic
--> $DIR/indexing_slicing_index.rs:31:14
|
LL | const { &ARR[idx4()] }; // Ok, let rustc handle const contexts.
| ^^^^^^^^^^^
|
= help: consider using `.get(n)` or `.get_mut(n)` instead
= note: the suggestion might not be applicable in constant blocks
error: indexing may panic
--> $DIR/indexing_slicing_index.rs:38:5
|
LL | v[0];
| ^^^^ help: consider using `.get(n)` or `.get_mut(n)` instead
| ^^^^
|
= help: consider using `.get(n)` or `.get_mut(n)` instead
error: indexing may panic
--> $DIR/indexing_slicing_index.rs:39:5
|
LL | v[10];
| ^^^^^ help: consider using `.get(n)` or `.get_mut(n)` instead
| ^^^^^
|
= help: consider using `.get(n)` or `.get_mut(n)` instead
error: indexing may panic
--> $DIR/indexing_slicing_index.rs:40:5
|
LL | v[1 << 3];
| ^^^^^^^^^ help: consider using `.get(n)` or `.get_mut(n)` instead
| ^^^^^^^^^
|
= help: consider using `.get(n)` or `.get_mut(n)` instead
error: indexing may panic
--> $DIR/indexing_slicing_index.rs:46:5
|
LL | v[N];
| ^^^^ help: consider using `.get(n)` or `.get_mut(n)` instead
| ^^^^
|
= help: consider using `.get(n)` or `.get_mut(n)` instead
error: indexing may panic
--> $DIR/indexing_slicing_index.rs:47:5
|
LL | v[M];
| ^^^^ help: consider using `.get(n)` or `.get_mut(n)` instead
| ^^^^
|
= help: consider using `.get(n)` or `.get_mut(n)` instead
error[E0080]: evaluation of constant value failed
--> $DIR/indexing_slicing_index.rs:10:24
@ -54,6 +117,6 @@ error[E0080]: evaluation of constant value failed
LL | const REF_ERR: &i32 = &ARR[idx4()]; // Ok, let rustc handle const contexts.
| ^^^^^^^^^^^ index out of bounds: the length is 2 but the index is 4
error: aborting due to 8 previous errors
error: aborting due to 14 previous errors
For more information about this error, try `rustc --explain E0080`.

View File

@ -2,39 +2,50 @@ error: slicing may panic
--> $DIR/indexing_slicing_slice.rs:12:6
|
LL | &x[index..];
| ^^^^^^^^^^ help: consider using `.get(n..)` or .get_mut(n..)` instead
| ^^^^^^^^^^
|
= help: consider using `.get(n..)` or .get_mut(n..)` instead
= note: `-D clippy::indexing-slicing` implied by `-D warnings`
error: slicing may panic
--> $DIR/indexing_slicing_slice.rs:13:6
|
LL | &x[..index];
| ^^^^^^^^^^ help: consider using `.get(..n)`or `.get_mut(..n)` instead
| ^^^^^^^^^^
|
= help: consider using `.get(..n)`or `.get_mut(..n)` instead
error: slicing may panic
--> $DIR/indexing_slicing_slice.rs:14:6
|
LL | &x[index_from..index_to];
| ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `.get(n..m)` or `.get_mut(n..m)` instead
| ^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider using `.get(n..m)` or `.get_mut(n..m)` instead
error: slicing may panic
--> $DIR/indexing_slicing_slice.rs:15:6
|
LL | &x[index_from..][..index_to]; // Two lint reports, one for [index_from..] and another for [..index_to].
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `.get(..n)`or `.get_mut(..n)` instead
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider using `.get(..n)`or `.get_mut(..n)` instead
error: slicing may panic
--> $DIR/indexing_slicing_slice.rs:15:6
|
LL | &x[index_from..][..index_to]; // Two lint reports, one for [index_from..] and another for [..index_to].
| ^^^^^^^^^^^^^^^ help: consider using `.get(n..)` or .get_mut(n..)` instead
| ^^^^^^^^^^^^^^^
|
= help: consider using `.get(n..)` or .get_mut(n..)` instead
error: slicing may panic
--> $DIR/indexing_slicing_slice.rs:16:6
|
LL | &x[5..][..10]; // Two lint reports, one for out of bounds [5..] and another for slicing [..10].
| ^^^^^^^^^^^^ help: consider using `.get(..n)`or `.get_mut(..n)` instead
| ^^^^^^^^^^^^
|
= help: consider using `.get(..n)`or `.get_mut(..n)` instead
error: range is out of bounds
--> $DIR/indexing_slicing_slice.rs:16:8
@ -48,13 +59,17 @@ error: slicing may panic
--> $DIR/indexing_slicing_slice.rs:17:6
|
LL | &x[0..][..3];
| ^^^^^^^^^^^ help: consider using `.get(..n)`or `.get_mut(..n)` instead
| ^^^^^^^^^^^
|
= help: consider using `.get(..n)`or `.get_mut(..n)` instead
error: slicing may panic
--> $DIR/indexing_slicing_slice.rs:18:6
|
LL | &x[1..][..5];
| ^^^^^^^^^^^ help: consider using `.get(..n)`or `.get_mut(..n)` instead
| ^^^^^^^^^^^
|
= help: consider using `.get(..n)`or `.get_mut(..n)` instead
error: range is out of bounds
--> $DIR/indexing_slicing_slice.rs:25:12
@ -72,13 +87,17 @@ error: slicing may panic
--> $DIR/indexing_slicing_slice.rs:31:6
|
LL | &v[10..100];
| ^^^^^^^^^^ help: consider using `.get(n..m)` or `.get_mut(n..m)` instead
| ^^^^^^^^^^
|
= help: consider using `.get(n..m)` or `.get_mut(n..m)` instead
error: slicing may panic
--> $DIR/indexing_slicing_slice.rs:32:6
|
LL | &x[10..][..100]; // Two lint reports, one for [10..] and another for [..100].
| ^^^^^^^^^^^^^^ help: consider using `.get(..n)`or `.get_mut(..n)` instead
| ^^^^^^^^^^^^^^
|
= help: consider using `.get(..n)`or `.get_mut(..n)` instead
error: range is out of bounds
--> $DIR/indexing_slicing_slice.rs:32:8
@ -90,13 +109,17 @@ error: slicing may panic
--> $DIR/indexing_slicing_slice.rs:33:6
|
LL | &v[10..];
| ^^^^^^^ help: consider using `.get(n..)` or .get_mut(n..)` instead
| ^^^^^^^
|
= help: consider using `.get(n..)` or .get_mut(n..)` instead
error: slicing may panic
--> $DIR/indexing_slicing_slice.rs:34:6
|
LL | &v[..100];
| ^^^^^^^^ help: consider using `.get(..n)`or `.get_mut(..n)` instead
| ^^^^^^^^
|
= help: consider using `.get(..n)`or `.get_mut(..n)` instead
error: aborting due to 16 previous errors