forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#5266 - sinkuu:questionmark, r=flip1995
Lint `if let Some` and early return in question_mark lint Fixes rust-lang#5260 changelog: lint `if let Some` and early return in `question_mark` lint
- Loading branch information
Showing
5 changed files
with
244 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,96 @@ | ||
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; | ||
LL | | } | ||
| |_____^ help: replace_it_with: `a?;` | ||
| |_____^ help: replace it with: `a?;` | ||
| | ||
= 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; | ||
LL | | } | ||
| |_________^ help: replace_it_with: `(self.opt)?;` | ||
| |_________^ 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 | ||
LL | | } | ||
| |_________^ help: replace_it_with: `self.opt?;` | ||
| |_________^ 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() { | ||
| _________________^ | ||
LL | | return None; | ||
LL | | } else { | ||
LL | | self.opt | ||
LL | | }; | ||
| |_________^ help: replace_it_with: `Some(self.opt?)` | ||
| |_________^ help: replace it with: `Some(self.opt?)` | ||
|
||
error: this if-let-else may be rewritten with the `?` operator | ||
--> $DIR/question_mark.rs:64:17 | ||
| | ||
LL | let _ = if let Some(x) = self.opt { | ||
| _________________^ | ||
LL | | x | ||
LL | | } else { | ||
LL | | return None; | ||
LL | | }; | ||
| |_________^ help: replace it with: `self.opt?` | ||
|
||
error: this block may be rewritten with the `?` operator | ||
--> $DIR/question_mark.rs:72:9 | ||
--> $DIR/question_mark.rs:81:9 | ||
| | ||
LL | / if self.opt.is_none() { | ||
LL | | return None; | ||
LL | | } | ||
| |_________^ help: replace_it_with: `self.opt.as_ref()?;` | ||
| |_________^ help: replace it with: `self.opt.as_ref()?;` | ||
|
||
error: this block may be rewritten with the `?` operator | ||
--> $DIR/question_mark.rs:80:9 | ||
--> $DIR/question_mark.rs:89:9 | ||
| | ||
LL | / if self.opt.is_none() { | ||
LL | | return None; | ||
LL | | } | ||
| |_________^ help: replace_it_with: `self.opt.as_ref()?;` | ||
| |_________^ help: replace it with: `self.opt.as_ref()?;` | ||
|
||
error: this block may be rewritten with the `?` operator | ||
--> $DIR/question_mark.rs:88:9 | ||
--> $DIR/question_mark.rs:97:9 | ||
| | ||
LL | / if self.opt.is_none() { | ||
LL | | return None; | ||
LL | | } | ||
| |_________^ help: replace_it_with: `self.opt.as_ref()?;` | ||
| |_________^ help: replace it with: `self.opt.as_ref()?;` | ||
|
||
error: this if-let-else may be rewritten with the `?` operator | ||
--> $DIR/question_mark.rs:104:26 | ||
| | ||
LL | let v: &Vec<_> = if let Some(ref v) = self.opt { | ||
| __________________________^ | ||
LL | | v | ||
LL | | } else { | ||
LL | | return None; | ||
LL | | }; | ||
| |_________^ help: replace it with: `self.opt.as_ref()?` | ||
|
||
error: this if-let-else may be rewritten with the `?` operator | ||
--> $DIR/question_mark.rs:114:17 | ||
| | ||
LL | let v = if let Some(v) = self.opt { | ||
| _________________^ | ||
LL | | v | ||
LL | | } else { | ||
LL | | return None; | ||
LL | | }; | ||
| |_________^ help: replace it with: `self.opt?` | ||
|
||
error: aborting due to 7 previous errors | ||
error: aborting due to 10 previous errors | ||
|