about summary refs log tree commit diff
diff options
context:
space:
mode:
authornaosense <pingao777@gmail.com>2022-11-22 17:12:50 +0800
committernaosense <pingao777@gmail.com>2022-11-29 15:00:51 +0800
commitaed9497978ce0520147cf1cf100513bf049ddf50 (patch)
tree89f356b77aed5df23668d5114874c19c92afecb8
parent96f1385fdd75af35ea888eeb68dc3631d7637a2b (diff)
downloadrust-aed9497978ce0520147cf1cf100513bf049ddf50.tar.gz
rust-aed9497978ce0520147cf1cf100513bf049ddf50.zip
add test and stderr
-rw-r--r--clippy_lints/src/indexing_slicing.rs12
-rw-r--r--clippy_lints/src/lib.rs3
-rw-r--r--clippy_lints/src/utils/conf.rs2
-rw-r--r--tests/ui-toml/suppress_lint_in_const/clippy.toml1
-rw-r--r--tests/ui-toml/suppress_lint_in_const/test.rs15
-rw-r--r--tests/ui-toml/suppress_lint_in_const/test.stderr11
-rw-r--r--tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr1
7 files changed, 35 insertions, 10 deletions
diff --git a/clippy_lints/src/indexing_slicing.rs b/clippy_lints/src/indexing_slicing.rs
index 23beeb4458c..92facbbf034 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 3bac394582e..3b5bac644ef 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 1352632b3a6..4a0e135db83 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 00000000000..fd459ff5ac5
--- /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 00000000000..e5f4ca7cc90
--- /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 00000000000..b4f6fe0c024
--- /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 01a5e962c94..521af13fe03 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