@@ -73,12 +73,27 @@ predicate encode_decode_objectapi(Expr ex, ClassObject type) {
73
73
)
74
74
}
75
75
76
+ predicate encode_decode ( Expr ex , ClassValue type ) {
77
+ exists ( string name |
78
+ ex .( Call ) .getFunc ( ) .( Attribute ) .getName ( ) = name |
79
+ name = "encode" and type = ClassValue:: unicodeEncodeError ( )
80
+ or
81
+ name = "decode" and type = ClassValue:: unicodeDecodeError ( )
82
+ )
83
+ }
84
+
76
85
predicate small_handler_objectapi ( ExceptStmt ex , Stmt s , ClassObject type ) {
77
86
not exists ( ex .getTry ( ) .getStmt ( 1 ) ) and
78
87
s = ex .getTry ( ) .getStmt ( 0 ) and
79
88
ex .getType ( ) .refersTo ( type )
80
89
}
81
90
91
+ predicate small_handler ( ExceptStmt ex , Stmt s , ClassValue type ) {
92
+ not exists ( ex .getTry ( ) .getStmt ( 1 ) ) and
93
+ s = ex .getTry ( ) .getStmt ( 0 ) and
94
+ ex .getType ( ) .pointsTo ( type )
95
+ }
96
+
82
97
/** Holds if this exception handler is sufficiently small in scope to not need a comment
83
98
* as to what it is doing.
84
99
*/
@@ -95,12 +110,25 @@ predicate focussed_handler_objectapi(ExceptStmt ex) {
95
110
)
96
111
}
97
112
113
+ predicate focussed_handler ( ExceptStmt ex ) {
114
+ exists ( Stmt s , ClassValue type |
115
+ small_handler ( ex , s , type ) |
116
+ subscript ( s ) and type .getASuperType ( ) = ClassValue:: lookupError ( )
117
+ or
118
+ attribute_access ( s ) and type = ClassValue:: attributeError ( )
119
+ or
120
+ s .( ExprStmt ) .getValue ( ) instanceof Name and type = ClassValue:: nameError ( )
121
+ or
122
+ encode_decode ( s .( ExprStmt ) .getValue ( ) , type )
123
+ )
124
+ }
125
+
98
126
Try try_return ( ) {
99
127
not exists ( result .getStmt ( 1 ) ) and result .getStmt ( 0 ) instanceof Return
100
128
}
101
129
102
130
from ExceptStmt ex
103
131
where empty_except ( ex ) and no_else ( ex ) and no_comment ( ex ) and not non_local_control_flow ( ex )
104
132
and not ex .getTry ( ) = try_return ( ) and try_has_normal_exit ( ex .getTry ( ) ) and
105
- not focussed_handler_objectapi ( ex )
133
+ not focussed_handler ( ex )
106
134
select ex , "'except' clause does nothing but pass and there is no explanatory comment."
0 commit comments