From 96f1385fdd75af35ea888eeb68dc3631d7637a2b Mon Sep 17 00:00:00 2001 From: naosense Date: Mon, 21 Nov 2022 14:36:56 +0800 Subject: [PATCH 1/7] add `suppress_lint_in_const` conf --- clippy_lints/src/indexing_slicing.rs | 19 ++++++++++++++++--- clippy_lints/src/lib.rs | 3 +++ clippy_lints/src/utils/conf.rs | 4 ++++ 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/clippy_lints/src/indexing_slicing.rs b/clippy_lints/src/indexing_slicing.rs index 4cd7dff4cfd7..23beeb4458c7 100644 --- a/clippy_lints/src/indexing_slicing.rs +++ b/clippy_lints/src/indexing_slicing.rs @@ -82,11 +82,24 @@ declare_clippy_lint! { "indexing/slicing usage" } +#[derive(Copy, Clone)] +pub struct IndexingSlicing { + suppress_lint_in_const: bool, +} + +impl IndexingSlicing { + pub fn new(suppress_lint_in_const: bool) -> Self { + Self { + suppress_lint_in_const, + } + } +} + declare_lint_pass!(IndexingSlicing => [INDEXING_SLICING, OUT_OF_BOUNDS_INDEXING]); impl<'tcx> LateLintPass<'tcx> for IndexingSlicing { fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { - if cx.tcx.hir().is_inside_const_context(expr.hir_id) { + if self.suppress_lint_in_const && cx.tcx.hir().is_inside_const_context(expr.hir_id) { return; } @@ -146,7 +159,7 @@ impl<'tcx> LateLintPass<'tcx> for IndexingSlicing { // Catchall non-range index, i.e., [n] or [n << m] if let ty::Array(..) = ty.kind() { // Index is a const block. - if let ExprKind::ConstBlock(..) = index.kind { + if self.suppress_lint_in_const && let ExprKind::ConstBlock(..) = index.kind { return; } // Index is a constant uint. @@ -191,7 +204,7 @@ fn to_const_range(cx: &LateContext<'_>, range: higher::Range<'_>, array_size: u1 } else { Some(x) } - }, + } Some(_) => None, None => Some(array_size), }; diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index 3fe39488ab82..3bac394582e0 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -562,7 +562,9 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: let avoid_breaking_exported_api = conf.avoid_breaking_exported_api; let allow_expect_in_tests = conf.allow_expect_in_tests; let allow_unwrap_in_tests = conf.allow_unwrap_in_tests; + let suppress_lint_in_const = conf.suppress_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(methods::Methods::new( avoid_breaking_exported_api, @@ -684,6 +686,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: 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(indexing_slicing::IndexingSlicing)); + store.register_late_pass(|_| Box::new(indexing_slicing::IndexingSlicing::new(suppress_lint_in_const))); 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(redundant_clone::RedundantClone)); diff --git a/clippy_lints/src/utils/conf.rs b/clippy_lints/src/utils/conf.rs index b6dc8cd7ab11..1352632b3a65 100644 --- a/clippy_lints/src/utils/conf.rs +++ b/clippy_lints/src/utils/conf.rs @@ -406,6 +406,10 @@ define_Conf! { /// /// Whether to allow mixed uninlined format args, e.g. `format!("{} {}", a, foo.bar)` (allow_mixed_uninlined_format_args: bool = true), + /// Lint: SUPPRESS_LINT + /// + /// Whether to suppress lint in const function + (suppress_lint_in_const: bool = true), } /// Search for the configuration file. From aed9497978ce0520147cf1cf100513bf049ddf50 Mon Sep 17 00:00:00 2001 From: naosense Date: Tue, 22 Nov 2022 17:12:50 +0800 Subject: [PATCH 2/7] add test and stderr --- clippy_lints/src/indexing_slicing.rs | 12 +++++------- clippy_lints/src/lib.rs | 3 +-- clippy_lints/src/utils/conf.rs | 2 +- tests/ui-toml/suppress_lint_in_const/clippy.toml | 1 + tests/ui-toml/suppress_lint_in_const/test.rs | 15 +++++++++++++++ tests/ui-toml/suppress_lint_in_const/test.stderr | 11 +++++++++++ .../toml_unknown_key/conf_unknown_key.stderr | 1 + 7 files changed, 35 insertions(+), 10 deletions(-) create mode 100644 tests/ui-toml/suppress_lint_in_const/clippy.toml create mode 100644 tests/ui-toml/suppress_lint_in_const/test.rs create mode 100644 tests/ui-toml/suppress_lint_in_const/test.stderr diff --git a/clippy_lints/src/indexing_slicing.rs b/clippy_lints/src/indexing_slicing.rs index 23beeb4458c7..92facbbf0341 100644 --- a/clippy_lints/src/indexing_slicing.rs +++ b/clippy_lints/src/indexing_slicing.rs @@ -7,7 +7,7 @@ use rustc_ast::ast::RangeLimits; use rustc_hir::{Expr, ExprKind}; use rustc_lint::{LateContext, LateLintPass}; use rustc_middle::ty; -use rustc_session::{declare_lint_pass, declare_tool_lint}; +use rustc_session::{declare_tool_lint, impl_lint_pass}; declare_clippy_lint! { /// ### What it does @@ -82,6 +82,8 @@ declare_clippy_lint! { "indexing/slicing usage" } +impl_lint_pass!(IndexingSlicing => [INDEXING_SLICING, OUT_OF_BOUNDS_INDEXING]); + #[derive(Copy, Clone)] pub struct IndexingSlicing { suppress_lint_in_const: bool, @@ -89,14 +91,10 @@ pub struct IndexingSlicing { impl IndexingSlicing { pub fn new(suppress_lint_in_const: bool) -> Self { - Self { - suppress_lint_in_const, - } + Self { suppress_lint_in_const } } } -declare_lint_pass!(IndexingSlicing => [INDEXING_SLICING, OUT_OF_BOUNDS_INDEXING]); - impl<'tcx> LateLintPass<'tcx> for IndexingSlicing { 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) { @@ -204,7 +202,7 @@ fn to_const_range(cx: &LateContext<'_>, range: higher::Range<'_>, array_size: u1 } else { Some(x) } - } + }, Some(_) => None, None => Some(array_size), }; diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index 3bac394582e0..3b5bac644ef5 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -685,8 +685,7 @@ 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(neg_cmp_op_on_partial_ord::NoNegCompOpForPartialOrd)); store.register_late_pass(|_| Box::new(unwrap::Unwrap)); - store.register_late_pass(|_| Box::new(indexing_slicing::IndexingSlicing)); - store.register_late_pass(|_| Box::new(indexing_slicing::IndexingSlicing::new(suppress_lint_in_const))); + store.register_late_pass(move |_| Box::new(indexing_slicing::IndexingSlicing::new(suppress_lint_in_const))); 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(redundant_clone::RedundantClone)); diff --git a/clippy_lints/src/utils/conf.rs b/clippy_lints/src/utils/conf.rs index 1352632b3a65..4a0e135db835 100644 --- a/clippy_lints/src/utils/conf.rs +++ b/clippy_lints/src/utils/conf.rs @@ -406,7 +406,7 @@ define_Conf! { /// /// Whether to allow mixed uninlined format args, e.g. `format!("{} {}", a, foo.bar)` (allow_mixed_uninlined_format_args: bool = true), - /// Lint: SUPPRESS_LINT + /// Lint: INDEXING_SLICING /// /// Whether to suppress lint in const function (suppress_lint_in_const: bool = true), diff --git a/tests/ui-toml/suppress_lint_in_const/clippy.toml b/tests/ui-toml/suppress_lint_in_const/clippy.toml new file mode 100644 index 000000000000..fd459ff5ac54 --- /dev/null +++ b/tests/ui-toml/suppress_lint_in_const/clippy.toml @@ -0,0 +1 @@ +suppress-lint-in-const = false \ No newline at end of file diff --git a/tests/ui-toml/suppress_lint_in_const/test.rs b/tests/ui-toml/suppress_lint_in_const/test.rs new file mode 100644 index 000000000000..e5f4ca7cc902 --- /dev/null +++ b/tests/ui-toml/suppress_lint_in_const/test.rs @@ -0,0 +1,15 @@ +#![warn(clippy::indexing_slicing)] + +/// An opaque integer representation +pub struct Integer<'a> { + /// The underlying data + value: &'a [u8], +} +impl<'a> Integer<'a> { + // Check whether `self` holds a negative number or not + pub const fn is_negative(&self) -> bool { + self.value[0] & 0b1000_0000 != 0 + } +} + +fn main() {} diff --git a/tests/ui-toml/suppress_lint_in_const/test.stderr b/tests/ui-toml/suppress_lint_in_const/test.stderr new file mode 100644 index 000000000000..b4f6fe0c024b --- /dev/null +++ b/tests/ui-toml/suppress_lint_in_const/test.stderr @@ -0,0 +1,11 @@ +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 + = note: `-D clippy::indexing-slicing` implied by `-D warnings` + +error: aborting due to previous error + diff --git a/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr b/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr index 01a5e962c949..521af13fe03d 100644 --- a/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr +++ b/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr @@ -35,6 +35,7 @@ error: error reading Clippy's configuration file `$DIR/clippy.toml`: unknown fie pass-by-value-size-limit single-char-binding-names-threshold standard-macro-braces + suppress-lint-in-const third-party too-large-for-stack too-many-arguments-threshold From 4528aec7e9a4fdce65a5dfec3a06aad5ee1950dd Mon Sep 17 00:00:00 2001 From: naosense Date: Wed, 23 Nov 2022 16:26:25 +0800 Subject: [PATCH 3/7] update config and suggest --- clippy_lints/src/indexing_slicing.rs | 45 ++++++++++++------ clippy_lints/src/lib.rs | 8 +++- clippy_lints/src/utils/conf.rs | 8 +++- .../suppress_lint_in_const/clippy.toml | 2 +- .../suppress_lint_in_const/test.stderr | 3 +- .../toml_unknown_key/conf_unknown_key.stderr | 2 +- tests/ui/indexing_slicing_index.stderr | 23 +++------ tests/ui/indexing_slicing_slice.stderr | 47 +++++-------------- 8 files changed, 63 insertions(+), 75 deletions(-) diff --git a/clippy_lints/src/indexing_slicing.rs b/clippy_lints/src/indexing_slicing.rs index 92facbbf0341..dfea0bf18d10 100644 --- a/clippy_lints/src/indexing_slicing.rs +++ b/clippy_lints/src/indexing_slicing.rs @@ -1,9 +1,10 @@ //! lint on indexing and slicing operations 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 rustc_ast::ast::RangeLimits; +use rustc_errors::Applicability; use rustc_hir::{Expr, ExprKind}; use rustc_lint::{LateContext, LateLintPass}; use rustc_middle::ty; @@ -86,18 +87,20 @@ impl_lint_pass!(IndexingSlicing => [INDEXING_SLICING, OUT_OF_BOUNDS_INDEXING]); #[derive(Copy, Clone)] pub struct IndexingSlicing { - suppress_lint_in_const: bool, + suppress_restriction_lint_in_const: bool, } impl IndexingSlicing { - pub fn new(suppress_lint_in_const: bool) -> Self { - Self { suppress_lint_in_const } + pub fn new(suppress_restriction_lint_in_const: bool) -> Self { + Self { + suppress_restriction_lint_in_const, + } } } impl<'tcx> LateLintPass<'tcx> for IndexingSlicing { 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; } @@ -152,12 +155,19 @@ impl<'tcx> LateLintPass<'tcx> for IndexingSlicing { (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 { // Catchall non-range index, i.e., [n] or [n << m] if let ty::Array(..) = ty.kind() { // 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; } // Index is a constant uint. @@ -167,14 +177,19 @@ impl<'tcx> LateLintPass<'tcx> for IndexingSlicing { } } - span_lint_and_help( - cx, - INDEXING_SLICING, - expr.span, - "indexing may panic", - None, - "consider using `.get(n)` or `.get_mut(n)` instead", - ); + 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, + ); + }); } } } diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index 3b5bac644ef5..2dc3a0822774 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -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 allow_expect_in_tests = conf.allow_expect_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 |_| { @@ -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(neg_cmp_op_on_partial_ord::NoNegCompOpForPartialOrd)); 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(ptr_offset_with_cast::PtrOffsetWithCast)); store.register_late_pass(|_| Box::new(redundant_clone::RedundantClone)); diff --git a/clippy_lints/src/utils/conf.rs b/clippy_lints/src/utils/conf.rs index 4a0e135db835..0022044ea417 100644 --- a/clippy_lints/src/utils/conf.rs +++ b/clippy_lints/src/utils/conf.rs @@ -408,8 +408,12 @@ define_Conf! { (allow_mixed_uninlined_format_args: bool = true), /// Lint: INDEXING_SLICING /// - /// Whether to suppress lint in const function - (suppress_lint_in_const: bool = true), + /// Whether to suppress a restriction lint in constant code. In same + /// 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. diff --git a/tests/ui-toml/suppress_lint_in_const/clippy.toml b/tests/ui-toml/suppress_lint_in_const/clippy.toml index fd459ff5ac54..d458f53a73dd 100644 --- a/tests/ui-toml/suppress_lint_in_const/clippy.toml +++ b/tests/ui-toml/suppress_lint_in_const/clippy.toml @@ -1 +1 @@ -suppress-lint-in-const = false \ No newline at end of file +suppress-restriction-lint-in-const = false \ No newline at end of file diff --git a/tests/ui-toml/suppress_lint_in_const/test.stderr b/tests/ui-toml/suppress_lint_in_const/test.stderr index b4f6fe0c024b..4e4583ab33cf 100644 --- a/tests/ui-toml/suppress_lint_in_const/test.stderr +++ b/tests/ui-toml/suppress_lint_in_const/test.stderr @@ -2,9 +2,8 @@ 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: `-D clippy::indexing-slicing` implied by `-D warnings` error: aborting due to previous error diff --git a/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr b/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr index 521af13fe03d..6ef2abb15b28 100644 --- a/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr +++ b/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr @@ -35,7 +35,7 @@ error: error reading Clippy's configuration file `$DIR/clippy.toml`: unknown fie pass-by-value-size-limit single-char-binding-names-threshold standard-macro-braces - suppress-lint-in-const + suppress-restriction-lint-in-const third-party too-large-for-stack too-many-arguments-threshold diff --git a/tests/ui/indexing_slicing_index.stderr b/tests/ui/indexing_slicing_index.stderr index d8b6e3f1262b..30fb69907958 100644 --- a/tests/ui/indexing_slicing_index.stderr +++ b/tests/ui/indexing_slicing_index.stderr @@ -14,50 +14,39 @@ error: indexing may panic --> $DIR/indexing_slicing_index.rs:22:5 | 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: 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 diff --git a/tests/ui/indexing_slicing_slice.stderr b/tests/ui/indexing_slicing_slice.stderr index dc54bd41365d..ac2cc009a22a 100644 --- a/tests/ui/indexing_slicing_slice.stderr +++ b/tests/ui/indexing_slicing_slice.stderr @@ -2,50 +2,39 @@ 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 @@ -59,17 +48,13 @@ 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 @@ -87,17 +72,13 @@ 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 @@ -109,17 +90,13 @@ 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 From c9bf4b75cea2259ad927228c762d4cf323628ae5 Mon Sep 17 00:00:00 2001 From: naosense Date: Wed, 23 Nov 2022 16:43:42 +0800 Subject: [PATCH 4/7] resolve conflicts --- clippy_lints/src/lib.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index 2dc3a0822774..5ce70de88104 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -564,7 +564,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: let allow_unwrap_in_tests = conf.allow_unwrap_in_tests; 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(methods::Methods::new( avoid_breaking_exported_api, From 1fc98c51dfd771e5cd45c2feee0649ae299ab18b Mon Sep 17 00:00:00 2001 From: naosense Date: Wed, 23 Nov 2022 16:50:05 +0800 Subject: [PATCH 5/7] change default value --- clippy_lints/src/utils/conf.rs | 2 +- tests/ui-toml/suppress_lint_in_const/clippy.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/clippy_lints/src/utils/conf.rs b/clippy_lints/src/utils/conf.rs index 0022044ea417..f5f0e3ef48cf 100644 --- a/clippy_lints/src/utils/conf.rs +++ b/clippy_lints/src/utils/conf.rs @@ -413,7 +413,7 @@ define_Conf! { /// 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), + (suppress_restriction_lint_in_const: bool = false), } /// Search for the configuration file. diff --git a/tests/ui-toml/suppress_lint_in_const/clippy.toml b/tests/ui-toml/suppress_lint_in_const/clippy.toml index d458f53a73dd..d6b6fc7f2688 100644 --- a/tests/ui-toml/suppress_lint_in_const/clippy.toml +++ b/tests/ui-toml/suppress_lint_in_const/clippy.toml @@ -1 +1 @@ -suppress-restriction-lint-in-const = false \ No newline at end of file +suppress-restriction-lint-in-const = false From 67a94135cbce195078efcbf18ac2347a1d734e68 Mon Sep 17 00:00:00 2001 From: naosense Date: Thu, 24 Nov 2022 09:43:11 +0800 Subject: [PATCH 6/7] change note style --- clippy_lints/src/indexing_slicing.rs | 29 +++---- .../suppress_lint_in_const/test.stderr | 4 +- tests/ui/indexing_slicing_index.stderr | 79 +++++++++++++++++-- tests/ui/indexing_slicing_slice.stderr | 47 ++++++++--- 4 files changed, 120 insertions(+), 39 deletions(-) diff --git a/clippy_lints/src/indexing_slicing.rs b/clippy_lints/src/indexing_slicing.rs index dfea0bf18d10..1a8ac43ac894 100644 --- a/clippy_lints/src/indexing_slicing.rs +++ b/clippy_lints/src/indexing_slicing.rs @@ -4,7 +4,6 @@ use clippy_utils::consts::{constant, Constant}; 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 @@ impl<'tcx> LateLintPass<'tcx> for IndexingSlicing { } 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 @@ impl<'tcx> LateLintPass<'tcx> for IndexingSlicing { }; 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 @@ impl<'tcx> LateLintPass<'tcx> for IndexingSlicing { } 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); + } }); } } diff --git a/tests/ui-toml/suppress_lint_in_const/test.stderr b/tests/ui-toml/suppress_lint_in_const/test.stderr index 4e4583ab33cf..c2acfed559d0 100644 --- a/tests/ui-toml/suppress_lint_in_const/test.stderr +++ b/tests/ui-toml/suppress_lint_in_const/test.stderr @@ -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 diff --git a/tests/ui/indexing_slicing_index.stderr b/tests/ui/indexing_slicing_index.stderr index 30fb69907958..84e1f65623c3 100644 --- a/tests/ui/indexing_slicing_index.stderr +++ b/tests/ui/indexing_slicing_index.stderr @@ -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`. diff --git a/tests/ui/indexing_slicing_slice.stderr b/tests/ui/indexing_slicing_slice.stderr index ac2cc009a22a..dc54bd41365d 100644 --- a/tests/ui/indexing_slicing_slice.stderr +++ b/tests/ui/indexing_slicing_slice.stderr @@ -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 From eec5039f09bc7992fbacaf0778adf85dcad54997 Mon Sep 17 00:00:00 2001 From: naosense Date: Mon, 28 Nov 2022 17:40:00 +0800 Subject: [PATCH 7/7] fix test --- clippy_lints/src/indexing_slicing.rs | 2 +- .../suppress_lint_in_const/clippy.toml | 2 +- tests/ui-toml/suppress_lint_in_const/test.rs | 49 ++++++++++++- .../suppress_lint_in_const/test.stderr | 70 +++++++++++++++++-- tests/ui/indexing_slicing_index.rs | 6 +- tests/ui/indexing_slicing_index.stderr | 28 ++------ 6 files changed, 122 insertions(+), 35 deletions(-) diff --git a/clippy_lints/src/indexing_slicing.rs b/clippy_lints/src/indexing_slicing.rs index 1a8ac43ac894..eebfb753a0c5 100644 --- a/clippy_lints/src/indexing_slicing.rs +++ b/clippy_lints/src/indexing_slicing.rs @@ -166,7 +166,7 @@ impl<'tcx> LateLintPass<'tcx> for IndexingSlicing { // Catchall non-range index, i.e., [n] or [n << m] if let ty::Array(..) = ty.kind() { // Index is a const block. - if self.suppress_restriction_lint_in_const && let ExprKind::ConstBlock(..) = index.kind { + if let ExprKind::ConstBlock(..) = index.kind { return; } // Index is a constant uint. diff --git a/tests/ui-toml/suppress_lint_in_const/clippy.toml b/tests/ui-toml/suppress_lint_in_const/clippy.toml index d6b6fc7f2688..1b9384d7e3ee 100644 --- a/tests/ui-toml/suppress_lint_in_const/clippy.toml +++ b/tests/ui-toml/suppress_lint_in_const/clippy.toml @@ -1 +1 @@ -suppress-restriction-lint-in-const = false +suppress-restriction-lint-in-const = true diff --git a/tests/ui-toml/suppress_lint_in_const/test.rs b/tests/ui-toml/suppress_lint_in_const/test.rs index e5f4ca7cc902..5a2df9f6c5d9 100644 --- a/tests/ui-toml/suppress_lint_in_const/test.rs +++ b/tests/ui-toml/suppress_lint_in_const/test.rs @@ -1,4 +1,51 @@ +#![feature(inline_const)] #![warn(clippy::indexing_slicing)] +// We also check the out_of_bounds_indexing lint here, because it lints similar things and +// we want to avoid false positives. +#![warn(clippy::out_of_bounds_indexing)] +#![allow(unconditional_panic, clippy::no_effect, clippy::unnecessary_operation)] + +const ARR: [i32; 2] = [1, 2]; +const REF: &i32 = &ARR[idx()]; // Ok, should not produce stderr, since `suppress-restriction-lint-in-const` is set true. +const REF_ERR: &i32 = &ARR[idx4()]; // Ok, let rustc handle const contexts. + +const fn idx() -> usize { + 1 +} +const fn idx4() -> usize { + 4 +} + +fn main() { + let x = [1, 2, 3, 4]; + let index: usize = 1; + x[index]; + x[4]; // Ok, let rustc's `unconditional_panic` lint handle `usize` indexing on arrays. + x[1 << 3]; // Ok, let rustc's `unconditional_panic` lint handle `usize` indexing on arrays. + + x[0]; // Ok, should not produce stderr. + x[3]; // Ok, should not produce stderr. + x[const { idx() }]; // Ok, should not produce stderr. + x[const { idx4() }]; // Ok, let rustc's `unconditional_panic` lint handle `usize` indexing on arrays. + const { &ARR[idx()] }; // Ok, should not produce stderr, since `suppress-restriction-lint-in-const` is set true. + const { &ARR[idx4()] }; // Ok, should not produce stderr, since `suppress-restriction-lint-in-const` is set true. + + let y = &x; + y[0]; // Ok, referencing shouldn't affect this lint. See the issue 6021 + y[4]; // Ok, rustc will handle references too. + + let v = vec![0; 5]; + v[0]; + v[10]; + v[1 << 3]; + + const N: usize = 15; // Out of bounds + const M: usize = 3; // In bounds + x[N]; // Ok, let rustc's `unconditional_panic` lint handle `usize` indexing on arrays. + x[M]; // Ok, should not produce stderr. + v[N]; + v[M]; +} /// An opaque integer representation pub struct Integer<'a> { @@ -11,5 +58,3 @@ impl<'a> Integer<'a> { self.value[0] & 0b1000_0000 != 0 } } - -fn main() {} diff --git a/tests/ui-toml/suppress_lint_in_const/test.stderr b/tests/ui-toml/suppress_lint_in_const/test.stderr index c2acfed559d0..bc178b7e1319 100644 --- a/tests/ui-toml/suppress_lint_in_const/test.stderr +++ b/tests/ui-toml/suppress_lint_in_const/test.stderr @@ -1,12 +1,70 @@ -error: indexing may panic - --> $DIR/test.rs:11:9 +error[E0080]: evaluation of `main::{constant#3}` failed + --> $DIR/test.rs:31:14 | -LL | self.value[0] & 0b1000_0000 != 0 - | ^^^^^^^^^^^^^ +LL | const { &ARR[idx4()] }; // Ok, should not produce stderr, since `suppress-restriction-lint-in-const` is set true. + | ^^^^^^^^^^^ index out of bounds: the length is 2 but the index is 4 + +note: erroneous constant used + --> $DIR/test.rs:31:5 + | +LL | const { &ARR[idx4()] }; // Ok, should not produce stderr, since `suppress-restriction-lint-in-const` is set true. + | ^^^^^^^^^^^^^^^^^^^^^^ + +error: indexing may panic + --> $DIR/test.rs:22:5 + | +LL | x[index]; + | ^^^^^^^^ | = 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 +error: indexing may panic + --> $DIR/test.rs:38:5 + | +LL | v[0]; + | ^^^^ + | + = help: consider using `.get(n)` or `.get_mut(n)` instead +error: indexing may panic + --> $DIR/test.rs:39:5 + | +LL | v[10]; + | ^^^^^ + | + = help: consider using `.get(n)` or `.get_mut(n)` instead + +error: indexing may panic + --> $DIR/test.rs:40:5 + | +LL | v[1 << 3]; + | ^^^^^^^^^ + | + = help: consider using `.get(n)` or `.get_mut(n)` instead + +error: indexing may panic + --> $DIR/test.rs:46:5 + | +LL | v[N]; + | ^^^^ + | + = help: consider using `.get(n)` or `.get_mut(n)` instead + +error: indexing may panic + --> $DIR/test.rs:47:5 + | +LL | v[M]; + | ^^^^ + | + = help: consider using `.get(n)` or `.get_mut(n)` instead + +error[E0080]: evaluation of constant value failed + --> $DIR/test.rs:10:24 + | +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 + +For more information about this error, try `rustc --explain E0080`. diff --git a/tests/ui/indexing_slicing_index.rs b/tests/ui/indexing_slicing_index.rs index 4476e0eb9220..26abc9edb5e4 100644 --- a/tests/ui/indexing_slicing_index.rs +++ b/tests/ui/indexing_slicing_index.rs @@ -6,7 +6,7 @@ #![allow(unconditional_panic, clippy::no_effect, clippy::unnecessary_operation)] const ARR: [i32; 2] = [1, 2]; -const REF: &i32 = &ARR[idx()]; // Ok, should not produce stderr. +const REF: &i32 = &ARR[idx()]; // This should be linted, since `suppress-restriction-lint-in-const` default is false. const REF_ERR: &i32 = &ARR[idx4()]; // Ok, let rustc handle const contexts. const fn idx() -> usize { @@ -27,8 +27,8 @@ fn main() { x[3]; // Ok, should not produce stderr. x[const { idx() }]; // Ok, should not produce stderr. x[const { idx4() }]; // Ok, let rustc's `unconditional_panic` lint handle `usize` indexing on arrays. - const { &ARR[idx()] }; // Ok, should not produce stderr. - const { &ARR[idx4()] }; // Ok, let rustc handle const contexts. + const { &ARR[idx()] }; // This should be linted, since `suppress-restriction-lint-in-const` default is false. + const { &ARR[idx4()] }; // This should be linted, since `suppress-restriction-lint-in-const` default is false. let y = &x; y[0]; // Ok, referencing shouldn't affect this lint. See the issue 6021 diff --git a/tests/ui/indexing_slicing_index.stderr b/tests/ui/indexing_slicing_index.stderr index 84e1f65623c3..8fd77913a3fd 100644 --- a/tests/ui/indexing_slicing_index.stderr +++ b/tests/ui/indexing_slicing_index.stderr @@ -1,7 +1,7 @@ error: indexing may panic --> $DIR/indexing_slicing_index.rs:9:20 | -LL | const REF: &i32 = &ARR[idx()]; // Ok, should not produce stderr. +LL | const REF: &i32 = &ARR[idx()]; // This should be linted, since `suppress-restriction-lint-in-const` default is false. | ^^^^^^^^^^ | = help: consider using `.get(n)` or `.get_mut(n)` instead @@ -20,13 +20,13 @@ LL | const REF_ERR: &i32 = &ARR[idx4()]; // Ok, let rustc handle const contexts. error[E0080]: evaluation of `main::{constant#3}` failed --> $DIR/indexing_slicing_index.rs:31:14 | -LL | const { &ARR[idx4()] }; // Ok, let rustc handle const contexts. +LL | const { &ARR[idx4()] }; // This should be linted, since `suppress-restriction-lint-in-const` default is false. | ^^^^^^^^^^^ index out of bounds: the length is 2 but the index is 4 note: erroneous constant used --> $DIR/indexing_slicing_index.rs:31:5 | -LL | const { &ARR[idx4()] }; // Ok, let rustc handle const contexts. +LL | const { &ARR[idx4()] }; // This should be linted, since `suppress-restriction-lint-in-const` default is false. | ^^^^^^^^^^^^^^^^^^^^^^ error: indexing may panic @@ -37,26 +37,10 @@ LL | x[index]; | = 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. +LL | const { &ARR[idx()] }; // This should be linted, since `suppress-restriction-lint-in-const` default is false. | ^^^^^^^^^^ | = help: consider using `.get(n)` or `.get_mut(n)` instead @@ -65,7 +49,7 @@ LL | const { &ARR[idx()] }; // Ok, should not produce stderr. error: indexing may panic --> $DIR/indexing_slicing_index.rs:31:14 | -LL | const { &ARR[idx4()] }; // Ok, let rustc handle const contexts. +LL | const { &ARR[idx4()] }; // This should be linted, since `suppress-restriction-lint-in-const` default is false. | ^^^^^^^^^^^ | = help: consider using `.get(n)` or `.get_mut(n)` instead @@ -117,6 +101,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 14 previous errors +error: aborting due to 12 previous errors For more information about this error, try `rustc --explain E0080`.