]> git.lizzy.rs Git - rust.git/commitdiff
fix `needless_question_mark` not considering async fn
authordswij <dswijj@gmail.com>
Tue, 18 Jan 2022 10:41:00 +0000 (18:41 +0800)
committerDharma Saputra Wijaya <dswijj@gmail.com>
Sun, 23 Jan 2022 12:31:27 +0000 (20:31 +0800)
clippy_lints/src/needless_question_mark.rs
tests/ui/needless_question_mark.fixed
tests/ui/needless_question_mark.rs
tests/ui/needless_question_mark.stderr

index 0e7ae43ce2dd53df347a380c4321f24b50f6cd51..d4c823d1c1ab77324efb77f0076831ff8131eb0e 100644 (file)
@@ -4,7 +4,7 @@
 use if_chain::if_chain;
 use rustc_errors::Applicability;
 use rustc_hir::LangItem::{OptionSome, ResultOk};
-use rustc_hir::{Body, Expr, ExprKind, LangItem, MatchSource, QPath};
+use rustc_hir::{AsyncGeneratorKind, Block, Body, Expr, ExprKind, GeneratorKind, LangItem, MatchSource, QPath};
 use rustc_lint::{LateContext, LateLintPass};
 use rustc_middle::ty::TyS;
 use rustc_session::{declare_lint_pass, declare_tool_lint};
@@ -88,7 +88,26 @@ fn check_expr(&mut self, cx: &LateContext<'_>, expr: &'_ Expr<'_>) {
     }
 
     fn check_body(&mut self, cx: &LateContext<'_>, body: &'_ Body<'_>) {
-        check(cx, body.value.peel_blocks());
+        if let Some(GeneratorKind::Async(AsyncGeneratorKind::Fn)) = body.generator_kind {
+            if let ExprKind::Block(
+                Block {
+                    expr:
+                        Some(Expr {
+                            kind: ExprKind::DropTemps(async_body),
+                            ..
+                        }),
+                    ..
+                },
+                _,
+            ) = body.value.kind
+            {
+                if let ExprKind::Block(Block { expr: Some(expr), .. }, ..) = async_body.kind {
+                    check(cx, expr);
+                }
+            }
+        } else {
+            check(cx, body.value.peel_blocks());
+        }
     }
 }
 
index f1fc81aa12b9e7cb6b325f7142a9027602f18483..ba9d15e59d0e45376374c23b07b87c661def3a17 100644 (file)
@@ -125,3 +125,16 @@ pub fn test2() {
     let x = Some(3);
     let _x = some_and_qmark_in_macro!(x?);
 }
+
+async fn async_option_bad(to: TO) -> Option<usize> {
+    let _ = Some(3);
+    to.magic
+}
+
+async fn async_deref_ref(s: Option<&String>) -> Option<&str> {
+    Some(s?)
+}
+
+async fn async_result_bad(s: TR) -> Result<usize, bool> {
+    s.magic
+}
index 44a0c5f61b5d5f71c9d6d7ebdc3eea35bb423572..3a6523e8fe872a94b172c851d1c997f9a1ed9350 100644 (file)
@@ -125,3 +125,16 @@ pub fn test2() {
     let x = Some(3);
     let _x = some_and_qmark_in_macro!(x?);
 }
+
+async fn async_option_bad(to: TO) -> Option<usize> {
+    let _ = Some(3);
+    Some(to.magic?)
+}
+
+async fn async_deref_ref(s: Option<&String>) -> Option<&str> {
+    Some(s?)
+}
+
+async fn async_result_bad(s: TR) -> Result<usize, bool> {
+    Ok(s.magic?)
+}
index 57c3d48c7611c2b1cad980070081cce9bb574aa2..f8308e24e7712e62adbf9b248757a3c9b0b32abb 100644 (file)
@@ -77,5 +77,17 @@ LL |     let _x = some_and_qmark_in_macro!(x?);
    |
    = note: this error originates in the macro `some_and_qmark_in_macro` (in Nightly builds, run with -Z macro-backtrace for more info)
 
-error: aborting due to 12 previous errors
+error: question mark operator is useless here
+  --> $DIR/needless_question_mark.rs:131:5
+   |
+LL |     Some(to.magic?)
+   |     ^^^^^^^^^^^^^^^ help: try removing question mark and `Some()`: `to.magic`
+
+error: question mark operator is useless here
+  --> $DIR/needless_question_mark.rs:139:5
+   |
+LL |     Ok(s.magic?)
+   |     ^^^^^^^^^^^^ help: try removing question mark and `Ok()`: `s.magic`
+
+error: aborting due to 14 previous errors