Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

question_mark: don't add as_ref() for a call expression #5481

Merged
merged 1 commit into from
Apr 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions clippy_lints/src/question_mark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,12 @@ impl QuestionMark {
replacement = Some(format!("Some({}?)", receiver_str));
}
}
} else if Self::moves_by_default(cx, subject) {
replacement = Some(format!("{}.as_ref()?;", receiver_str));
} else if Self::moves_by_default(cx, subject)
&& !matches!(subject.kind, ExprKind::Call(..) | ExprKind::MethodCall(..))
{
replacement = Some(format!("{}.as_ref()?;", receiver_str));
} else {
replacement = Some(format!("{}?;", receiver_str));
replacement = Some(format!("{}?;", receiver_str));
}

if let Some(replacement_str) = replacement {
Expand Down
12 changes: 12 additions & 0 deletions tests/ui/question_mark.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ impl MoveStruct {
}
}

fn func() -> Option<i32> {
fn f() -> Option<String> {
Some(String::new())
}

f()?;

Some(0)
}

fn main() {
some_func(Some(42));
some_func(None);
Expand All @@ -110,4 +120,6 @@ fn main() {

let so = SeemsOption::Some(45);
returns_something_similar_to_option(so);

func();
}
14 changes: 14 additions & 0 deletions tests/ui/question_mark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,18 @@ impl MoveStruct {
}
}

fn func() -> Option<i32> {
fn f() -> Option<String> {
Some(String::new())
}

if f().is_none() {
return None;
}

Some(0)
}

fn main() {
some_func(Some(42));
some_func(None);
Expand All @@ -138,4 +150,6 @@ fn main() {

let so = SeemsOption::Some(45);
returns_something_similar_to_option(so);

func();
}
10 changes: 9 additions & 1 deletion tests/ui/question_mark.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,13 @@ LL | | return None;
LL | | };
| |_________^ help: replace it with: `self.opt?`

error: aborting due to 10 previous errors
error: this block may be rewritten with the `?` operator
--> $DIR/question_mark.rs:129:5
|
LL | / if f().is_none() {
LL | | return None;
LL | | }
| |_____^ help: replace it with: `f()?;`

error: aborting due to 11 previous errors