Skip to content

Commit d976d8a

Browse files
committed
Auto merge of rust-lang#8311 - dswij:8277, r=llogiq
fix `needless_question_mark` not considering async fn closes rust-lang#8277 changelog: [`needless_question_mark`] Fix FN on async functions
2 parents 788a8bc + a052766 commit d976d8a

File tree

4 files changed

+60
-3
lines changed

4 files changed

+60
-3
lines changed

clippy_lints/src/needless_question_mark.rs

+21-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use clippy_utils::source::snippet;
44
use if_chain::if_chain;
55
use rustc_errors::Applicability;
66
use rustc_hir::LangItem::{OptionSome, ResultOk};
7-
use rustc_hir::{Body, Expr, ExprKind, LangItem, MatchSource, QPath};
7+
use rustc_hir::{AsyncGeneratorKind, Block, Body, Expr, ExprKind, GeneratorKind, LangItem, MatchSource, QPath};
88
use rustc_lint::{LateContext, LateLintPass};
99
use rustc_middle::ty::TyS;
1010
use rustc_session::{declare_lint_pass, declare_tool_lint};
@@ -88,7 +88,26 @@ impl LateLintPass<'_> for NeedlessQuestionMark {
8888
}
8989

9090
fn check_body(&mut self, cx: &LateContext<'_>, body: &'_ Body<'_>) {
91-
check(cx, body.value.peel_blocks());
91+
if let Some(GeneratorKind::Async(AsyncGeneratorKind::Fn)) = body.generator_kind {
92+
if let ExprKind::Block(
93+
Block {
94+
expr:
95+
Some(Expr {
96+
kind: ExprKind::DropTemps(async_body),
97+
..
98+
}),
99+
..
100+
},
101+
_,
102+
) = body.value.kind
103+
{
104+
if let ExprKind::Block(Block { expr: Some(expr), .. }, ..) = async_body.kind {
105+
check(cx, expr);
106+
}
107+
}
108+
} else {
109+
check(cx, body.value.peel_blocks());
110+
}
92111
}
93112
}
94113

tests/ui/needless_question_mark.fixed

+13
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,16 @@ pub fn test2() {
125125
let x = Some(3);
126126
let _x = some_and_qmark_in_macro!(x?);
127127
}
128+
129+
async fn async_option_bad(to: TO) -> Option<usize> {
130+
let _ = Some(3);
131+
to.magic
132+
}
133+
134+
async fn async_deref_ref(s: Option<&String>) -> Option<&str> {
135+
Some(s?)
136+
}
137+
138+
async fn async_result_bad(s: TR) -> Result<usize, bool> {
139+
s.magic
140+
}

tests/ui/needless_question_mark.rs

+13
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,16 @@ pub fn test2() {
125125
let x = Some(3);
126126
let _x = some_and_qmark_in_macro!(x?);
127127
}
128+
129+
async fn async_option_bad(to: TO) -> Option<usize> {
130+
let _ = Some(3);
131+
Some(to.magic?)
132+
}
133+
134+
async fn async_deref_ref(s: Option<&String>) -> Option<&str> {
135+
Some(s?)
136+
}
137+
138+
async fn async_result_bad(s: TR) -> Result<usize, bool> {
139+
Ok(s.magic?)
140+
}

tests/ui/needless_question_mark.stderr

+13-1
Original file line numberDiff line numberDiff line change
@@ -77,5 +77,17 @@ LL | let _x = some_and_qmark_in_macro!(x?);
7777
|
7878
= note: this error originates in the macro `some_and_qmark_in_macro` (in Nightly builds, run with -Z macro-backtrace for more info)
7979

80-
error: aborting due to 12 previous errors
80+
error: question mark operator is useless here
81+
--> $DIR/needless_question_mark.rs:131:5
82+
|
83+
LL | Some(to.magic?)
84+
| ^^^^^^^^^^^^^^^ help: try removing question mark and `Some()`: `to.magic`
85+
86+
error: question mark operator is useless here
87+
--> $DIR/needless_question_mark.rs:139:5
88+
|
89+
LL | Ok(s.magic?)
90+
| ^^^^^^^^^^^^ help: try removing question mark and `Ok()`: `s.magic`
91+
92+
error: aborting due to 14 previous errors
8193

0 commit comments

Comments
 (0)