diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
commit | 698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch) | |
tree | 173a775858bd501c378080a10dca74132f05bc50 /src/tools/clippy/tests/ui/question_mark.fixed | |
parent | Initial commit. (diff) | |
download | rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip |
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/tools/clippy/tests/ui/question_mark.fixed')
-rw-r--r-- | src/tools/clippy/tests/ui/question_mark.fixed | 210 |
1 files changed, 210 insertions, 0 deletions
diff --git a/src/tools/clippy/tests/ui/question_mark.fixed b/src/tools/clippy/tests/ui/question_mark.fixed new file mode 100644 index 000000000..c4c9c8214 --- /dev/null +++ b/src/tools/clippy/tests/ui/question_mark.fixed @@ -0,0 +1,210 @@ +// run-rustfix +#![allow(unreachable_code)] +#![allow(dead_code)] +#![allow(clippy::unnecessary_wraps)] + +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 func() -> Option<i32> { + fn f() -> Option<String> { + Some(String::new()) + } + + f()?; + + Some(0) +} + +fn func_returning_result() -> Result<i32, i32> { + Ok(1) +} + +fn result_func(x: Result<i32, i32>) -> Result<i32, i32> { + let _ = x?; + + x?; + + // No warning + let y = if let Ok(x) = x { + x + } else { + return Err(0); + }; + + // issue #7859 + // no warning + let _ = if let Ok(x) = func_returning_result() { + x + } else { + return Err(0); + }; + + // no warning + if func_returning_result().is_err() { + return func_returning_result(); + } + + Ok(y) +} + +// see issue #8019 +pub enum NotOption { + None, + First, + AfterFirst, +} + +fn obj(_: i32) -> Result<(), NotOption> { + Err(NotOption::First) +} + +fn f() -> NotOption { + if obj(2).is_err() { + return NotOption::None; + } + NotOption::First +} + +fn do_something() {} + +fn err_immediate_return() -> Result<i32, i32> { + func_returning_result()?; + Ok(1) +} + +fn err_immediate_return_and_do_something() -> Result<i32, i32> { + func_returning_result()?; + do_something(); + Ok(1) +} + +// No warning +fn no_immediate_return() -> Result<i32, i32> { + if let Err(err) = func_returning_result() { + do_something(); + return Err(err); + } + Ok(1) +} + +// No warning +fn mixed_result_and_option() -> Option<i32> { + if let Err(err) = func_returning_result() { + return Some(err); + } + None +} + +// No warning +fn else_if_check() -> Result<i32, i32> { + if true { + Ok(1) + } else if let Err(e) = func_returning_result() { + Err(e) + } else { + Err(-1) + } +} + +// No warning +#[allow(clippy::manual_map)] +#[rustfmt::skip] +fn option_map() -> Option<bool> { + if let Some(a) = Some(false) { + Some(!a) + } else { + None + } +} + +fn main() {} |