@@ -824,7 +824,17 @@ declare_lint! {
824
824
"`if`, `match`, `while` and `return` do not need parentheses"
825
825
}
826
826
827
- declare_lint_pass ! ( UnusedParens => [ UNUSED_PARENS ] ) ;
827
+ pub struct UnusedParens {
828
+ with_self_ty_parens : bool ,
829
+ }
830
+
831
+ impl UnusedParens {
832
+ pub fn new ( ) -> Self {
833
+ Self { with_self_ty_parens : false }
834
+ }
835
+ }
836
+
837
+ impl_lint_pass ! ( UnusedParens => [ UNUSED_PARENS ] ) ;
828
838
829
839
impl UnusedDelimLint for UnusedParens {
830
840
const DELIM_STR : & ' static str = "parentheses" ;
@@ -999,36 +1009,58 @@ impl EarlyLintPass for UnusedParens {
999
1009
}
1000
1010
1001
1011
fn check_ty ( & mut self , cx : & EarlyContext < ' _ > , ty : & ast:: Ty ) {
1002
- if let ast:: TyKind :: Paren ( r) = & ty. kind {
1003
- match & r. kind {
1004
- ast:: TyKind :: TraitObject ( ..) => { }
1005
- ast:: TyKind :: BareFn ( b) if b. generic_params . len ( ) > 0 => { }
1006
- ast:: TyKind :: ImplTrait ( _, bounds) if bounds. len ( ) > 1 => { }
1007
- ast:: TyKind :: Array ( _, len) => {
1008
- self . check_unused_delims_expr (
1009
- cx,
1010
- & len. value ,
1011
- UnusedDelimsCtx :: ArrayLenExpr ,
1012
- false ,
1013
- None ,
1014
- None ,
1015
- ) ;
1016
- }
1017
- _ => {
1018
- let spans = if let Some ( r) = r. span . find_ancestor_inside ( ty. span ) {
1019
- Some ( ( ty. span . with_hi ( r. lo ( ) ) , ty. span . with_lo ( r. hi ( ) ) ) )
1020
- } else {
1021
- None
1022
- } ;
1023
- self . emit_unused_delims ( cx, ty. span , spans, "type" , ( false , false ) ) ;
1012
+ match & ty. kind {
1013
+ ast:: TyKind :: Array ( _, len) => {
1014
+ self . check_unused_delims_expr (
1015
+ cx,
1016
+ & len. value ,
1017
+ UnusedDelimsCtx :: ArrayLenExpr ,
1018
+ false ,
1019
+ None ,
1020
+ None ,
1021
+ ) ;
1022
+ }
1023
+ ast:: TyKind :: Paren ( r) => {
1024
+ match & r. kind {
1025
+ ast:: TyKind :: TraitObject ( ..) => { }
1026
+ ast:: TyKind :: BareFn ( b)
1027
+ if self . with_self_ty_parens && b. generic_params . len ( ) > 0 => { }
1028
+ ast:: TyKind :: ImplTrait ( _, bounds) if bounds. len ( ) > 1 => { }
1029
+ _ => {
1030
+ let spans = if let Some ( r) = r. span . find_ancestor_inside ( ty. span ) {
1031
+ Some ( ( ty. span . with_hi ( r. lo ( ) ) , ty. span . with_lo ( r. hi ( ) ) ) )
1032
+ } else {
1033
+ None
1034
+ } ;
1035
+ self . emit_unused_delims ( cx, ty. span , spans, "type" , ( false , false ) ) ;
1036
+ }
1024
1037
}
1038
+ self . with_self_ty_parens = false ;
1025
1039
}
1040
+ _ => { }
1026
1041
}
1027
1042
}
1028
1043
1029
1044
fn check_item ( & mut self , cx : & EarlyContext < ' _ > , item : & ast:: Item ) {
1030
1045
<Self as UnusedDelimLint >:: check_item ( self , cx, item)
1031
1046
}
1047
+
1048
+ fn enter_where_predicate ( & mut self , _: & EarlyContext < ' _ > , pred : & ast:: WherePredicate ) {
1049
+ use rustc_ast:: { WhereBoundPredicate , WherePredicate } ;
1050
+ if let WherePredicate :: BoundPredicate ( WhereBoundPredicate {
1051
+ bounded_ty,
1052
+ bound_generic_params,
1053
+ ..
1054
+ } ) = pred &&
1055
+ let ast:: TyKind :: Paren ( _) = & bounded_ty. kind &&
1056
+ bound_generic_params. is_empty ( ) {
1057
+ self . with_self_ty_parens = true ;
1058
+ }
1059
+ }
1060
+
1061
+ fn exit_where_predicate ( & mut self , _: & EarlyContext < ' _ > , _: & ast:: WherePredicate ) {
1062
+ assert ! ( !self . with_self_ty_parens) ;
1063
+ }
1032
1064
}
1033
1065
1034
1066
declare_lint ! {
0 commit comments