about summary refs log tree commit diff
diff options
context:
space:
mode:
authorSamuel Tardieu <sam@rfc1149.net>2025-02-09 17:45:09 +0100
committerSamuel Tardieu <sam@rfc1149.net>2025-02-09 18:18:02 +0100
commit657dda7b5091001a22722b4eeacdeddfe62b3b54 (patch)
tree3f3c135318dadd911e20d4181a249de7e7ba81e5
parentd7fd1c8e3c1460fcf2aef3bc335bc547e4acc314 (diff)
downloadrust-657dda7b5091001a22722b4eeacdeddfe62b3b54.tar.gz
rust-657dda7b5091001a22722b4eeacdeddfe62b3b54.zip
`let_and_return`: look for non-static references in expansion as well
One cannot avoid descending into expansion results when looking for
non-static references, or there is a risk of false negative which would
then trigger the `let_and_return` lint.
-rw-r--r--clippy_lints/src/returns.rs4
-rw-r--r--tests/ui/let_and_return.fixed10
-rw-r--r--tests/ui/let_and_return.rs10
3 files changed, 22 insertions, 2 deletions
diff --git a/clippy_lints/src/returns.rs b/clippy_lints/src/returns.rs
index a1cf16e6ce9..3fcab6a766b 100644
--- a/clippy_lints/src/returns.rs
+++ b/clippy_lints/src/returns.rs
@@ -1,7 +1,7 @@
 use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_hir_and_then};
 use clippy_utils::source::{SpanRangeExt, snippet_with_context};
 use clippy_utils::sugg::has_enclosing_paren;
-use clippy_utils::visitors::{Descend, for_each_expr};
+use clippy_utils::visitors::for_each_expr;
 use clippy_utils::{
     binary_expr_needs_parentheses, fn_def_id, is_from_proc_macro, is_inside_let_else, is_res_lang_ctor,
     leaks_droppable_temporary_with_limited_lifetime, path_res, path_to_local_id, span_contains_cfg,
@@ -483,7 +483,7 @@ fn last_statement_borrows<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>)
         {
             ControlFlow::Break(())
         } else {
-            ControlFlow::Continue(Descend::from(!e.span.from_expansion()))
+            ControlFlow::Continue(())
         }
     })
     .is_some()
diff --git a/tests/ui/let_and_return.fixed b/tests/ui/let_and_return.fixed
index b68b41cdca2..e22e66eb522 100644
--- a/tests/ui/let_and_return.fixed
+++ b/tests/ui/let_and_return.fixed
@@ -244,4 +244,14 @@ fn issue12801() {
     }
 }
 
+// Do not lint
+fn issue14164() -> Result<u32, ()> {
+    let v = std::cell::RefCell::new(Some(vec![1]));
+    let r = match &*v.borrow() {
+        Some(v) => Ok(Ok(v[0])),
+        None => Ok(Ok(0)),
+    }?;
+    r
+}
+
 fn main() {}
diff --git a/tests/ui/let_and_return.rs b/tests/ui/let_and_return.rs
index 6b9035f9428..f9da55946c0 100644
--- a/tests/ui/let_and_return.rs
+++ b/tests/ui/let_and_return.rs
@@ -244,4 +244,14 @@ fn issue12801() {
     }
 }
 
+// Do not lint
+fn issue14164() -> Result<u32, ()> {
+    let v = std::cell::RefCell::new(Some(vec![1]));
+    let r = match &*v.borrow() {
+        Some(v) => Ok(Ok(v[0])),
+        None => Ok(Ok(0)),
+    }?;
+    r
+}
+
 fn main() {}