]> git.lizzy.rs Git - rust.git/blobdiff - tests/ui/unnecessary_lazy_eval.fixed
[`excessive_bools`] lint trait functions even without bodies
[rust.git] / tests / ui / unnecessary_lazy_eval.fixed
index 65fcdc43061bfe8557441050c3cc73c54121ccb5..ce4a82e021745d45e532710d2c93cd82be091988 100644 (file)
@@ -1,9 +1,13 @@
 // run-rustfix
+// aux-build: proc_macro_with_span.rs
 #![warn(clippy::unnecessary_lazy_evaluations)]
 #![allow(clippy::redundant_closure)]
 #![allow(clippy::bind_instead_of_map)]
 #![allow(clippy::map_identity)]
 
+extern crate proc_macro_with_span;
+use proc_macro_with_span::with_span;
+
 struct Deep(Option<usize>);
 
 #[derive(Copy, Clone)]
@@ -21,6 +25,14 @@ fn some_call<T: Default>() -> T {
     T::default()
 }
 
+struct Issue9427(i32);
+
+impl Drop for Issue9427 {
+    fn drop(&mut self) {
+        println!("{}", self.0);
+    }
+}
+
 fn main() {
     let astronomers_pi = 10;
     let ext_arr: [usize; 1] = [2];
@@ -30,6 +42,7 @@ fn main() {
     let ext_opt = Some(42);
     let nested_opt = Some(Some(42));
     let nested_tuple_opt = Some(Some((42, 43)));
+    let cond = true;
 
     // Should lint - Option
     let _ = opt.unwrap_or(2);
@@ -42,6 +55,7 @@ fn main() {
     let _ = opt.get_or_insert(2);
     let _ = opt.ok_or(2);
     let _ = nested_tuple_opt.unwrap_or(Some((1, 2)));
+    let _ = cond.then_some(astronomers_pi);
 
     // Cases when unwrap is not called on a simple variable
     let _ = Some(10).unwrap_or(2);
@@ -71,6 +85,9 @@ fn main() {
     let _ = deep.0.or_else(|| some_call());
     let _ = opt.ok_or_else(|| ext_arr[0]);
 
+    // Should not lint - bool
+    let _ = (0 == 1).then(|| Issue9427(0)); // Issue9427 has a significant drop
+
     // should not lint, bind_instead_of_map takes priority
     let _ = Some(10).and_then(|idx| Some(ext_arr[idx]));
     let _ = Some(10).and_then(|idx| Some(idx));
@@ -128,3 +145,9 @@ fn main() {
     let _: Result<usize, usize> = res.and_then(|x| Err(x));
     let _: Result<usize, usize> = res.or_else(|err| Ok(err));
 }
+
+#[allow(unused)]
+fn issue9485() {
+    // should not lint, is in proc macro
+    with_span!(span Some(42).unwrap_or_else(|| 2););
+}