update config and suggest
This commit is contained in:
parent
aed9497978
commit
4528aec7e9
@ -1,9 +1,10 @@
|
|||||||
//! lint on indexing and slicing operations
|
//! lint on indexing and slicing operations
|
||||||
|
|
||||||
use clippy_utils::consts::{constant, Constant};
|
use clippy_utils::consts::{constant, Constant};
|
||||||
use clippy_utils::diagnostics::{span_lint, span_lint_and_help};
|
use clippy_utils::diagnostics::{span_lint, span_lint_and_then};
|
||||||
use clippy_utils::higher;
|
use clippy_utils::higher;
|
||||||
use rustc_ast::ast::RangeLimits;
|
use rustc_ast::ast::RangeLimits;
|
||||||
|
use rustc_errors::Applicability;
|
||||||
use rustc_hir::{Expr, ExprKind};
|
use rustc_hir::{Expr, ExprKind};
|
||||||
use rustc_lint::{LateContext, LateLintPass};
|
use rustc_lint::{LateContext, LateLintPass};
|
||||||
use rustc_middle::ty;
|
use rustc_middle::ty;
|
||||||
@ -86,18 +87,20 @@
|
|||||||
|
|
||||||
#[derive(Copy, Clone)]
|
#[derive(Copy, Clone)]
|
||||||
pub struct IndexingSlicing {
|
pub struct IndexingSlicing {
|
||||||
suppress_lint_in_const: bool,
|
suppress_restriction_lint_in_const: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl IndexingSlicing {
|
impl IndexingSlicing {
|
||||||
pub fn new(suppress_lint_in_const: bool) -> Self {
|
pub fn new(suppress_restriction_lint_in_const: bool) -> Self {
|
||||||
Self { suppress_lint_in_const }
|
Self {
|
||||||
|
suppress_restriction_lint_in_const,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'tcx> LateLintPass<'tcx> for IndexingSlicing {
|
impl<'tcx> LateLintPass<'tcx> for IndexingSlicing {
|
||||||
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
||||||
if self.suppress_lint_in_const && cx.tcx.hir().is_inside_const_context(expr.hir_id) {
|
if self.suppress_restriction_lint_in_const && cx.tcx.hir().is_inside_const_context(expr.hir_id) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -152,12 +155,19 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
|||||||
(None, None) => return, // [..] is ok.
|
(None, None) => return, // [..] is ok.
|
||||||
};
|
};
|
||||||
|
|
||||||
span_lint_and_help(cx, INDEXING_SLICING, expr.span, "slicing may panic", None, help_msg);
|
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);
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
// Catchall non-range index, i.e., [n] or [n << m]
|
// Catchall non-range index, i.e., [n] or [n << m]
|
||||||
if let ty::Array(..) = ty.kind() {
|
if let ty::Array(..) = ty.kind() {
|
||||||
// Index is a const block.
|
// Index is a const block.
|
||||||
if self.suppress_lint_in_const && let ExprKind::ConstBlock(..) = index.kind {
|
if self.suppress_restriction_lint_in_const && let ExprKind::ConstBlock(..) = index.kind {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// Index is a constant uint.
|
// Index is a constant uint.
|
||||||
@ -167,14 +177,19 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
span_lint_and_help(
|
span_lint_and_then(cx, INDEXING_SLICING, expr.span, "indexing may panic", |diag| {
|
||||||
cx,
|
let note = if cx.tcx.hir().is_inside_const_context(expr.hir_id) {
|
||||||
INDEXING_SLICING,
|
"the suggestion might not be applicable in constant blocks"
|
||||||
expr.span,
|
} else {
|
||||||
"indexing may panic",
|
""
|
||||||
None,
|
};
|
||||||
"consider using `.get(n)` or `.get_mut(n)` instead",
|
diag.span_suggestion(
|
||||||
);
|
expr.span,
|
||||||
|
"consider using `.get(n)` or `.get_mut(n)` instead",
|
||||||
|
note,
|
||||||
|
Applicability::MachineApplicable,
|
||||||
|
);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -562,7 +562,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
|
|||||||
let avoid_breaking_exported_api = conf.avoid_breaking_exported_api;
|
let avoid_breaking_exported_api = conf.avoid_breaking_exported_api;
|
||||||
let allow_expect_in_tests = conf.allow_expect_in_tests;
|
let allow_expect_in_tests = conf.allow_expect_in_tests;
|
||||||
let allow_unwrap_in_tests = conf.allow_unwrap_in_tests;
|
let allow_unwrap_in_tests = conf.allow_unwrap_in_tests;
|
||||||
let suppress_lint_in_const = conf.suppress_lint_in_const;
|
let suppress_restriction_lint_in_const = conf.suppress_restriction_lint_in_const;
|
||||||
store.register_late_pass(move |_| Box::new(approx_const::ApproxConstant::new(msrv())));
|
store.register_late_pass(move |_| Box::new(approx_const::ApproxConstant::new(msrv())));
|
||||||
store.register_late_pass(move |_| Box::new(approx_const::ApproxConstant::new(msrv)));
|
store.register_late_pass(move |_| Box::new(approx_const::ApproxConstant::new(msrv)));
|
||||||
store.register_late_pass(move |_| {
|
store.register_late_pass(move |_| {
|
||||||
@ -685,7 +685,11 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
|
|||||||
store.register_late_pass(|_| Box::new(inherent_impl::MultipleInherentImpl));
|
store.register_late_pass(|_| Box::new(inherent_impl::MultipleInherentImpl));
|
||||||
store.register_late_pass(|_| Box::new(neg_cmp_op_on_partial_ord::NoNegCompOpForPartialOrd));
|
store.register_late_pass(|_| Box::new(neg_cmp_op_on_partial_ord::NoNegCompOpForPartialOrd));
|
||||||
store.register_late_pass(|_| Box::new(unwrap::Unwrap));
|
store.register_late_pass(|_| Box::new(unwrap::Unwrap));
|
||||||
store.register_late_pass(move |_| Box::new(indexing_slicing::IndexingSlicing::new(suppress_lint_in_const)));
|
store.register_late_pass(move |_| {
|
||||||
|
Box::new(indexing_slicing::IndexingSlicing::new(
|
||||||
|
suppress_restriction_lint_in_const,
|
||||||
|
))
|
||||||
|
});
|
||||||
store.register_late_pass(|_| Box::new(non_copy_const::NonCopyConst));
|
store.register_late_pass(|_| Box::new(non_copy_const::NonCopyConst));
|
||||||
store.register_late_pass(|_| Box::new(ptr_offset_with_cast::PtrOffsetWithCast));
|
store.register_late_pass(|_| Box::new(ptr_offset_with_cast::PtrOffsetWithCast));
|
||||||
store.register_late_pass(|_| Box::new(redundant_clone::RedundantClone));
|
store.register_late_pass(|_| Box::new(redundant_clone::RedundantClone));
|
||||||
|
@ -408,8 +408,12 @@ pub(crate) fn get_configuration_metadata() -> Vec<ClippyConfiguration> {
|
|||||||
(allow_mixed_uninlined_format_args: bool = true),
|
(allow_mixed_uninlined_format_args: bool = true),
|
||||||
/// Lint: INDEXING_SLICING
|
/// Lint: INDEXING_SLICING
|
||||||
///
|
///
|
||||||
/// Whether to suppress lint in const function
|
/// Whether to suppress a restriction lint in constant code. In same
|
||||||
(suppress_lint_in_const: bool = true),
|
/// cases the restructured operation might not be unavoidable, as the
|
||||||
|
/// suggested counterparts are unavailable in constant code. This
|
||||||
|
/// configuration will cause restriction lints to trigger even
|
||||||
|
/// if no suggestion can be made.
|
||||||
|
(suppress_restriction_lint_in_const: bool = true),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Search for the configuration file.
|
/// Search for the configuration file.
|
||||||
|
@ -1 +1 @@
|
|||||||
suppress-lint-in-const = false
|
suppress-restriction-lint-in-const = false
|
@ -2,9 +2,8 @@ error: indexing may panic
|
|||||||
--> $DIR/test.rs:11:9
|
--> $DIR/test.rs:11:9
|
||||||
|
|
|
|
||||||
LL | self.value[0] & 0b1000_0000 != 0
|
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: `-D clippy::indexing-slicing` implied by `-D warnings`
|
= note: `-D clippy::indexing-slicing` implied by `-D warnings`
|
||||||
|
|
||||||
error: aborting due to previous error
|
error: aborting due to previous error
|
||||||
|
@ -35,7 +35,7 @@ error: error reading Clippy's configuration file `$DIR/clippy.toml`: unknown fie
|
|||||||
pass-by-value-size-limit
|
pass-by-value-size-limit
|
||||||
single-char-binding-names-threshold
|
single-char-binding-names-threshold
|
||||||
standard-macro-braces
|
standard-macro-braces
|
||||||
suppress-lint-in-const
|
suppress-restriction-lint-in-const
|
||||||
third-party
|
third-party
|
||||||
too-large-for-stack
|
too-large-for-stack
|
||||||
too-many-arguments-threshold
|
too-many-arguments-threshold
|
||||||
|
@ -14,50 +14,39 @@ error: indexing may panic
|
|||||||
--> $DIR/indexing_slicing_index.rs:22:5
|
--> $DIR/indexing_slicing_index.rs:22:5
|
||||||
|
|
|
|
||||||
LL | x[index];
|
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`
|
= note: `-D clippy::indexing-slicing` implied by `-D warnings`
|
||||||
|
|
||||||
error: indexing may panic
|
error: indexing may panic
|
||||||
--> $DIR/indexing_slicing_index.rs:38:5
|
--> $DIR/indexing_slicing_index.rs:38:5
|
||||||
|
|
|
|
||||||
LL | v[0];
|
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
|
error: indexing may panic
|
||||||
--> $DIR/indexing_slicing_index.rs:39:5
|
--> $DIR/indexing_slicing_index.rs:39:5
|
||||||
|
|
|
|
||||||
LL | v[10];
|
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
|
error: indexing may panic
|
||||||
--> $DIR/indexing_slicing_index.rs:40:5
|
--> $DIR/indexing_slicing_index.rs:40:5
|
||||||
|
|
|
|
||||||
LL | v[1 << 3];
|
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
|
error: indexing may panic
|
||||||
--> $DIR/indexing_slicing_index.rs:46:5
|
--> $DIR/indexing_slicing_index.rs:46:5
|
||||||
|
|
|
|
||||||
LL | v[N];
|
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
|
error: indexing may panic
|
||||||
--> $DIR/indexing_slicing_index.rs:47:5
|
--> $DIR/indexing_slicing_index.rs:47:5
|
||||||
|
|
|
|
||||||
LL | v[M];
|
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
|
error[E0080]: evaluation of constant value failed
|
||||||
--> $DIR/indexing_slicing_index.rs:10:24
|
--> $DIR/indexing_slicing_index.rs:10:24
|
||||||
|
@ -2,50 +2,39 @@ error: slicing may panic
|
|||||||
--> $DIR/indexing_slicing_slice.rs:12:6
|
--> $DIR/indexing_slicing_slice.rs:12:6
|
||||||
|
|
|
|
||||||
LL | &x[index..];
|
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`
|
= note: `-D clippy::indexing-slicing` implied by `-D warnings`
|
||||||
|
|
||||||
error: slicing may panic
|
error: slicing may panic
|
||||||
--> $DIR/indexing_slicing_slice.rs:13:6
|
--> $DIR/indexing_slicing_slice.rs:13:6
|
||||||
|
|
|
|
||||||
LL | &x[..index];
|
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
|
error: slicing may panic
|
||||||
--> $DIR/indexing_slicing_slice.rs:14:6
|
--> $DIR/indexing_slicing_slice.rs:14:6
|
||||||
|
|
|
|
||||||
LL | &x[index_from..index_to];
|
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
|
error: slicing may panic
|
||||||
--> $DIR/indexing_slicing_slice.rs:15:6
|
--> $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].
|
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
|
error: slicing may panic
|
||||||
--> $DIR/indexing_slicing_slice.rs:15:6
|
--> $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].
|
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
|
error: slicing may panic
|
||||||
--> $DIR/indexing_slicing_slice.rs:16:6
|
--> $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].
|
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
|
error: range is out of bounds
|
||||||
--> $DIR/indexing_slicing_slice.rs:16:8
|
--> $DIR/indexing_slicing_slice.rs:16:8
|
||||||
@ -59,17 +48,13 @@ error: slicing may panic
|
|||||||
--> $DIR/indexing_slicing_slice.rs:17:6
|
--> $DIR/indexing_slicing_slice.rs:17:6
|
||||||
|
|
|
|
||||||
LL | &x[0..][..3];
|
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
|
error: slicing may panic
|
||||||
--> $DIR/indexing_slicing_slice.rs:18:6
|
--> $DIR/indexing_slicing_slice.rs:18:6
|
||||||
|
|
|
|
||||||
LL | &x[1..][..5];
|
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
|
error: range is out of bounds
|
||||||
--> $DIR/indexing_slicing_slice.rs:25:12
|
--> $DIR/indexing_slicing_slice.rs:25:12
|
||||||
@ -87,17 +72,13 @@ error: slicing may panic
|
|||||||
--> $DIR/indexing_slicing_slice.rs:31:6
|
--> $DIR/indexing_slicing_slice.rs:31:6
|
||||||
|
|
|
|
||||||
LL | &v[10..100];
|
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
|
error: slicing may panic
|
||||||
--> $DIR/indexing_slicing_slice.rs:32:6
|
--> $DIR/indexing_slicing_slice.rs:32:6
|
||||||
|
|
|
|
||||||
LL | &x[10..][..100]; // Two lint reports, one for [10..] and another for [..100].
|
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
|
error: range is out of bounds
|
||||||
--> $DIR/indexing_slicing_slice.rs:32:8
|
--> $DIR/indexing_slicing_slice.rs:32:8
|
||||||
@ -109,17 +90,13 @@ error: slicing may panic
|
|||||||
--> $DIR/indexing_slicing_slice.rs:33:6
|
--> $DIR/indexing_slicing_slice.rs:33:6
|
||||||
|
|
|
|
||||||
LL | &v[10..];
|
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
|
error: slicing may panic
|
||||||
--> $DIR/indexing_slicing_slice.rs:34:6
|
--> $DIR/indexing_slicing_slice.rs:34:6
|
||||||
|
|
|
|
||||||
LL | &v[..100];
|
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
|
error: aborting due to 16 previous errors
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user