about summary refs log tree commit diff
diff options
context:
space:
mode:
authorLzu Tao <taolzu@gmail.com>2024-05-24 03:25:58 +0000
committerLzu Tao <taolzu@gmail.com>2024-06-08 16:50:25 +0700
commit2c61b4557688aca993f59d2170673e86c4df9101 (patch)
tree15aa5558c21c107ddb02f83a08b251a5664e52b2
parentb161dc659c87df29cc9738a49105124ab30638e8 (diff)
do not lint on indexing inside const contexts
-rw-r--r--clippy_lints/src/no_effect.rs7
-rw-r--r--tests/ui/unnecessary_operation.fixed4
-rw-r--r--tests/ui/unnecessary_operation.stderr14
3 files changed, 9 insertions, 16 deletions
diff --git a/clippy_lints/src/no_effect.rs b/clippy_lints/src/no_effect.rs
index dc017fa6634..139c33d3f4a 100644
--- a/clippy_lints/src/no_effect.rs
+++ b/clippy_lints/src/no_effect.rs
@@ -1,7 +1,9 @@
 use clippy_utils::diagnostics::{span_lint_hir, span_lint_hir_and_then};
 use clippy_utils::source::snippet_opt;
 use clippy_utils::ty::has_drop;
-use clippy_utils::{any_parent_is_automatically_derived, is_lint_allowed, path_to_local, peel_blocks};
+use clippy_utils::{
+    any_parent_is_automatically_derived, is_inside_always_const_context, is_lint_allowed, path_to_local, peel_blocks,
+};
 use rustc_errors::Applicability;
 use rustc_hir::def::{DefKind, Res};
 use rustc_hir::{
@@ -265,6 +267,9 @@ fn check_unnecessary_operation(cx: &LateContext<'_>, stmt: &Stmt<'_>) {
         && reduced.iter().all(|e| e.span.ctxt() == ctxt)
     {
         if let ExprKind::Index(..) = &expr.kind {
+            if is_inside_always_const_context(cx.tcx, expr.hir_id) {
+                return;
+            }
             let snippet =
                 if let (Some(arr), Some(func)) = (snippet_opt(cx, reduced[0].span), snippet_opt(cx, reduced[1].span)) {
                     format!("assert!({}.len() > {});", &arr, &func)
diff --git a/tests/ui/unnecessary_operation.fixed b/tests/ui/unnecessary_operation.fixed
index e54491ab023..5a3c49626a1 100644
--- a/tests/ui/unnecessary_operation.fixed
+++ b/tests/ui/unnecessary_operation.fixed
@@ -114,12 +114,12 @@ fn main() {
         break 'label
     };
     let () = const {
-        assert!([42, 55].len() > get_usize());
+        [42, 55][get_usize()];
     };
 }
 
 const _: () = {
-    assert!([42, 55].len() > get_usize());
+    [42, 55][get_usize()];
 };
 
 const fn foo() {
diff --git a/tests/ui/unnecessary_operation.stderr b/tests/ui/unnecessary_operation.stderr
index 3c031d006d4..036a9a44bba 100644
--- a/tests/ui/unnecessary_operation.stderr
+++ b/tests/ui/unnecessary_operation.stderr
@@ -120,22 +120,10 @@ LL | |     };
    | |______^ help: statement can be reduced to: `String::from("blah");`
 
 error: unnecessary operation
-  --> tests/ui/unnecessary_operation.rs:121:9
-   |
-LL |         [42, 55][get_usize()];
-   |         ^^^^^^^^^^^^^^^^^^^^^^ help: statement can be written as: `assert!([42, 55].len() > get_usize());`
-
-error: unnecessary operation
-  --> tests/ui/unnecessary_operation.rs:126:5
-   |
-LL |     [42, 55][get_usize()];
-   |     ^^^^^^^^^^^^^^^^^^^^^^ help: statement can be written as: `assert!([42, 55].len() > get_usize());`
-
-error: unnecessary operation
   --> tests/ui/unnecessary_operation.rs:130:5
    |
 LL |     [42, 55][get_usize()];
    |     ^^^^^^^^^^^^^^^^^^^^^^ help: statement can be written as: `assert!([42, 55].len() > get_usize());`
 
-error: aborting due to 22 previous errors
+error: aborting due to 20 previous errors