about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--clippy_lints/src/question_mark.rs5
-rw-r--r--tests/ui/question_mark.fixed113
-rw-r--r--tests/ui/question_mark.rs8
-rw-r--r--tests/ui/question_mark.stderr28
4 files changed, 136 insertions, 18 deletions
diff --git a/clippy_lints/src/question_mark.rs b/clippy_lints/src/question_mark.rs
index 5ad580aa052..3b030f95dc9 100644
--- a/clippy_lints/src/question_mark.rs
+++ b/clippy_lints/src/question_mark.rs
@@ -57,7 +57,8 @@ impl QuestionMark {
             if Self::is_option(cx, subject);
 
             then {
-                let receiver_str = &Sugg::hir(cx, subject, "..");
+                let mut applicability = Applicability::MachineApplicable;
+                let receiver_str = snippet_with_applicability(cx, subject.span, "..", &mut applicability);
                 let mut replacement: Option<String> = None;
                 if let Some(else_) = else_ {
                     if_chain! {
@@ -86,7 +87,7 @@ impl QuestionMark {
                                 expr.span,
                                 "replace it with",
                                 replacement_str,
-                                Applicability::MaybeIncorrect, // snippet
+                                applicability,
                             );
                         }
                     )
diff --git a/tests/ui/question_mark.fixed b/tests/ui/question_mark.fixed
new file mode 100644
index 00000000000..2c3e4989d53
--- /dev/null
+++ b/tests/ui/question_mark.fixed
@@ -0,0 +1,113 @@
+// run-rustfix
+#![allow(unreachable_code)]
+
+fn some_func(a: Option<u32>) -> Option<u32> {
+    a?;
+
+    a
+}
+
+fn some_other_func(a: Option<u32>) -> Option<u32> {
+    if a.is_none() {
+        return None;
+    } else {
+        return Some(0);
+    }
+    unreachable!()
+}
+
+pub enum SeemsOption<T> {
+    Some(T),
+    None,
+}
+
+impl<T> SeemsOption<T> {
+    pub fn is_none(&self) -> bool {
+        match *self {
+            SeemsOption::None => true,
+            SeemsOption::Some(_) => false,
+        }
+    }
+}
+
+fn returns_something_similar_to_option(a: SeemsOption<u32>) -> SeemsOption<u32> {
+    if a.is_none() {
+        return SeemsOption::None;
+    }
+
+    a
+}
+
+pub struct CopyStruct {
+    pub opt: Option<u32>,
+}
+
+impl CopyStruct {
+    #[rustfmt::skip]
+    pub fn func(&self) -> Option<u32> {
+        (self.opt)?;
+
+        self.opt?;
+
+        let _ = Some(self.opt?);
+
+        let _ = self.opt?;
+
+        self.opt
+    }
+}
+
+#[derive(Clone)]
+pub struct MoveStruct {
+    pub opt: Option<Vec<u32>>,
+}
+
+impl MoveStruct {
+    pub fn ref_func(&self) -> Option<Vec<u32>> {
+        self.opt.as_ref()?;
+
+        self.opt.clone()
+    }
+
+    pub fn mov_func_reuse(self) -> Option<Vec<u32>> {
+        self.opt.as_ref()?;
+
+        self.opt
+    }
+
+    pub fn mov_func_no_use(self) -> Option<Vec<u32>> {
+        self.opt.as_ref()?;
+        Some(Vec::new())
+    }
+
+    pub fn if_let_ref_func(self) -> Option<Vec<u32>> {
+        let v: &Vec<_> = self.opt.as_ref()?;
+
+        Some(v.clone())
+    }
+
+    pub fn if_let_mov_func(self) -> Option<Vec<u32>> {
+        let v = self.opt?;
+
+        Some(v)
+    }
+}
+
+fn main() {
+    some_func(Some(42));
+    some_func(None);
+    some_other_func(Some(42));
+
+    let copy_struct = CopyStruct { opt: Some(54) };
+    copy_struct.func();
+
+    let move_struct = MoveStruct {
+        opt: Some(vec![42, 1337]),
+    };
+    move_struct.ref_func();
+    move_struct.clone().mov_func_reuse();
+    move_struct.mov_func_no_use();
+
+    let so = SeemsOption::Some(45);
+    returns_something_similar_to_option(so);
+}
diff --git a/tests/ui/question_mark.rs b/tests/ui/question_mark.rs
index 77aa3976b79..24df7634435 100644
--- a/tests/ui/question_mark.rs
+++ b/tests/ui/question_mark.rs
@@ -1,3 +1,6 @@
+// run-rustfix
+#![allow(unreachable_code)]
+
 fn some_func(a: Option<u32>) -> Option<u32> {
     if a.is_none() {
         return None;
@@ -98,7 +101,7 @@ impl MoveStruct {
     }
 
     pub fn if_let_ref_func(self) -> Option<Vec<u32>> {
-        let mut v: &Vec<_> = if let Some(ref v) = self.opt {
+        let v: &Vec<_> = if let Some(ref v) = self.opt {
             v
         } else {
             return None;
@@ -108,7 +111,7 @@ impl MoveStruct {
     }
 
     pub fn if_let_mov_func(self) -> Option<Vec<u32>> {
-        let mut v = if let Some(v) = self.opt {
+        let v = if let Some(v) = self.opt {
             v
         } else {
             return None;
@@ -121,6 +124,7 @@ impl MoveStruct {
 fn main() {
     some_func(Some(42));
     some_func(None);
+    some_other_func(Some(42));
 
     let copy_struct = CopyStruct { opt: Some(54) };
     copy_struct.func();
diff --git a/tests/ui/question_mark.stderr b/tests/ui/question_mark.stderr
index dabba9842e4..97741069b50 100644
--- a/tests/ui/question_mark.stderr
+++ b/tests/ui/question_mark.stderr
@@ -1,5 +1,5 @@
 error: this block may be rewritten with the `?` operator
-  --> $DIR/question_mark.rs:2:5
+  --> $DIR/question_mark.rs:5:5
    |
 LL | /     if a.is_none() {
 LL | |         return None;
@@ -9,7 +9,7 @@ LL | |     }
    = note: `-D clippy::question-mark` implied by `-D warnings`
 
 error: this block may be rewritten with the `?` operator
-  --> $DIR/question_mark.rs:47:9
+  --> $DIR/question_mark.rs:50:9
    |
 LL | /         if (self.opt).is_none() {
 LL | |             return None;
@@ -17,7 +17,7 @@ LL | |         }
    | |_________^ help: replace it with: `(self.opt)?;`
 
 error: this block may be rewritten with the `?` operator
-  --> $DIR/question_mark.rs:51:9
+  --> $DIR/question_mark.rs:54:9
    |
 LL | /         if self.opt.is_none() {
 LL | |             return None
@@ -25,7 +25,7 @@ LL | |         }
    | |_________^ help: replace it with: `self.opt?;`
 
 error: this block may be rewritten with the `?` operator
-  --> $DIR/question_mark.rs:55:17
+  --> $DIR/question_mark.rs:58:17
    |
 LL |           let _ = if self.opt.is_none() {
    |  _________________^
@@ -36,7 +36,7 @@ LL | |         };
    | |_________^ help: replace it with: `Some(self.opt?)`
 
 error: this if-let-else may be rewritten with the `?` operator
-  --> $DIR/question_mark.rs:61:17
+  --> $DIR/question_mark.rs:64:17
    |
 LL |           let _ = if let Some(x) = self.opt {
    |  _________________^
@@ -47,7 +47,7 @@ LL | |         };
    | |_________^ help: replace it with: `self.opt?`
 
 error: this block may be rewritten with the `?` operator
-  --> $DIR/question_mark.rs:78:9
+  --> $DIR/question_mark.rs:81:9
    |
 LL | /         if self.opt.is_none() {
 LL | |             return None;
@@ -55,7 +55,7 @@ LL | |         }
    | |_________^ help: replace it with: `self.opt.as_ref()?;`
 
 error: this block may be rewritten with the `?` operator
-  --> $DIR/question_mark.rs:86:9
+  --> $DIR/question_mark.rs:89:9
    |
 LL | /         if self.opt.is_none() {
 LL | |             return None;
@@ -63,7 +63,7 @@ LL | |         }
    | |_________^ help: replace it with: `self.opt.as_ref()?;`
 
 error: this block may be rewritten with the `?` operator
-  --> $DIR/question_mark.rs:94:9
+  --> $DIR/question_mark.rs:97:9
    |
 LL | /         if self.opt.is_none() {
 LL | |             return None;
@@ -71,10 +71,10 @@ LL | |         }
    | |_________^ help: replace it with: `self.opt.as_ref()?;`
 
 error: this if-let-else may be rewritten with the `?` operator
-  --> $DIR/question_mark.rs:101:30
+  --> $DIR/question_mark.rs:104:26
    |
-LL |           let mut v: &Vec<_> = if let Some(ref v) = self.opt {
-   |  ______________________________^
+LL |           let v: &Vec<_> = if let Some(ref v) = self.opt {
+   |  __________________________^
 LL | |             v
 LL | |         } else {
 LL | |             return None;
@@ -82,10 +82,10 @@ LL | |         };
    | |_________^ help: replace it with: `self.opt.as_ref()?`
 
 error: this if-let-else may be rewritten with the `?` operator
-  --> $DIR/question_mark.rs:111:21
+  --> $DIR/question_mark.rs:114:17
    |
-LL |           let mut v = if let Some(v) = self.opt {
-   |  _____________________^
+LL |           let v = if let Some(v) = self.opt {
+   |  _________________^
 LL | |             v
 LL | |         } else {
 LL | |             return None;