]> git.lizzy.rs Git - rust.git/commitdiff
fix issues in unused lint
authoryukang <moorekang@gmail.com>
Sat, 14 Jan 2023 08:52:46 +0000 (16:52 +0800)
committeryukang <moorekang@gmail.com>
Sat, 14 Jan 2023 09:11:04 +0000 (17:11 +0800)
compiler/rustc_lint/src/early.rs
compiler/rustc_lint/src/lib.rs
compiler/rustc_lint/src/passes.rs
compiler/rustc_lint/src/unused.rs
library/std/src/io/error/repr_bitpacked.rs

index 3901751c79fb0853646685d88a56f04dcf947256..337a19dd024d2fec195601c77de253036df847bf 100644 (file)
@@ -248,13 +248,9 @@ fn visit_generics(&mut self, g: &'a ast::Generics) {
     }
 
     fn visit_where_predicate(&mut self, p: &'a ast::WherePredicate) {
-        use rustc_ast::{WhereBoundPredicate, WherePredicate};
-        if let WherePredicate::BoundPredicate(WhereBoundPredicate { bounded_ty, .. }) = p &&
-            let ast::TyKind::BareFn(b) = &bounded_ty.kind &&
-            b.generic_params.len() > 0 {
-                return;
-        }
+        lint_callback!(self, enter_where_predicate, p);
         ast_visit::walk_where_predicate(self, p);
+        lint_callback!(self, exit_where_predicate, p);
     }
 
     fn visit_poly_trait_ref(&mut self, t: &'a ast::PolyTraitRef) {
index 3d818154cb94ff48c1a7c33fd2a19d473b089588..d6be4da03286f75f1e4868e5bac454b345008a8b 100644 (file)
@@ -145,7 +145,7 @@ fn lint_mod(tcx: TyCtxt<'_>, module_def_id: LocalDefId) {
     [
         pub BuiltinCombinedEarlyLintPass,
         [
-            UnusedParens: UnusedParens,
+            UnusedParens: UnusedParens::new(),
             UnusedBraces: UnusedBraces,
             UnusedImportBraces: UnusedImportBraces,
             UnsafeCode: UnsafeCode,
index 5558156a4b9ef040a07ff54271328b64f31e2ba4..0bf01c4e567814f1f720de66362f310cf5c0f934 100644 (file)
@@ -171,6 +171,9 @@ macro_rules! early_lint_methods {
 
             /// Counterpart to `enter_lint_attrs`.
             fn exit_lint_attrs(a: &[ast::Attribute]);
+
+            fn enter_where_predicate(a: &ast::WherePredicate);
+            fn exit_where_predicate(a: &ast::WherePredicate);
         ]);
     )
 }
index 94a33138107171218c9f600b9b8c6c270e379d70..65f2644a858af4d06cb824b63d0089b53d9d2c7a 100644 (file)
@@ -824,7 +824,17 @@ fn check_item(&mut self, cx: &EarlyContext<'_>, item: &ast::Item) {
     "`if`, `match`, `while` and `return` do not need parentheses"
 }
 
-declare_lint_pass!(UnusedParens => [UNUSED_PARENS]);
+pub struct UnusedParens {
+    with_self_ty_parens: bool,
+}
+
+impl UnusedParens {
+    pub fn new() -> Self {
+        Self { with_self_ty_parens: false }
+    }
+}
+
+impl_lint_pass!(UnusedParens => [UNUSED_PARENS]);
 
 impl UnusedDelimLint for UnusedParens {
     const DELIM_STR: &'static str = "parentheses";
@@ -999,20 +1009,22 @@ fn check_arm(&mut self, cx: &EarlyContext<'_>, arm: &ast::Arm) {
     }
 
     fn check_ty(&mut self, cx: &EarlyContext<'_>, ty: &ast::Ty) {
+        if let ast::TyKind::Array(_, len) = &ty.kind {
+            self.check_unused_delims_expr(
+                cx,
+                &len.value,
+                UnusedDelimsCtx::ArrayLenExpr,
+                false,
+                None,
+                None,
+            );
+        }
         if let ast::TyKind::Paren(r) = &ty.kind {
             match &r.kind {
                 ast::TyKind::TraitObject(..) => {}
+                ast::TyKind::BareFn(b)
+                    if self.with_self_ty_parens && b.generic_params.len() > 0 => {}
                 ast::TyKind::ImplTrait(_, bounds) if bounds.len() > 1 => {}
-                ast::TyKind::Array(_, len) => {
-                    self.check_unused_delims_expr(
-                        cx,
-                        &len.value,
-                        UnusedDelimsCtx::ArrayLenExpr,
-                        false,
-                        None,
-                        None,
-                    );
-                }
                 _ => {
                     let spans = if let Some(r) = r.span.find_ancestor_inside(ty.span) {
                         Some((ty.span.with_hi(r.lo()), ty.span.with_lo(r.hi())))
@@ -1028,6 +1040,23 @@ fn check_ty(&mut self, cx: &EarlyContext<'_>, ty: &ast::Ty) {
     fn check_item(&mut self, cx: &EarlyContext<'_>, item: &ast::Item) {
         <Self as UnusedDelimLint>::check_item(self, cx, item)
     }
+
+    fn enter_where_predicate(&mut self, _: &EarlyContext<'_>, pred: &ast::WherePredicate) {
+        use rustc_ast::{WhereBoundPredicate, WherePredicate};
+        if let WherePredicate::BoundPredicate(WhereBoundPredicate {
+                bounded_ty,
+                bound_generic_params,
+                ..
+            }) = pred &&
+            let ast::TyKind::Paren(_) = &bounded_ty.kind &&
+            bound_generic_params.is_empty() {
+                self.with_self_ty_parens = true;
+        }
+    }
+
+    fn exit_where_predicate(&mut self, _: &EarlyContext<'_>, _: &ast::WherePredicate) {
+        self.with_self_ty_parens = false;
+    }
 }
 
 declare_lint! {
index 601c01c2128c816757c103d3a04f3c4c0dd8646c..3581484050dd1a4e506574b46d99ce609c5ed996 100644 (file)
@@ -374,10 +374,10 @@ macro_rules! static_assert {
 static_assert!(align_of::<SimpleMessage>() >= TAG_MASK + 1);
 static_assert!(align_of::<Custom>() >= TAG_MASK + 1);
 
-static_assert!(@usize_eq: (TAG_MASK & TAG_SIMPLE_MESSAGE), TAG_SIMPLE_MESSAGE);
-static_assert!(@usize_eq: (TAG_MASK & TAG_CUSTOM), TAG_CUSTOM);
-static_assert!(@usize_eq: (TAG_MASK & TAG_OS), TAG_OS);
-static_assert!(@usize_eq: (TAG_MASK & TAG_SIMPLE), TAG_SIMPLE);
+static_assert!(@usize_eq: TAG_MASK & TAG_SIMPLE_MESSAGE, TAG_SIMPLE_MESSAGE);
+static_assert!(@usize_eq: TAG_MASK & TAG_CUSTOM, TAG_CUSTOM);
+static_assert!(@usize_eq: TAG_MASK & TAG_OS, TAG_OS);
+static_assert!(@usize_eq: TAG_MASK & TAG_SIMPLE, TAG_SIMPLE);
 
 // This is obviously true (`TAG_CUSTOM` is `0b01`), but in `Repr::new_custom` we
 // offset a pointer by this value, and expect it to both be within the same