@@ -73,12 +73,27 @@ predicate encode_decode_objectapi(Expr ex, ClassObject type) {
7373 )
7474}
7575
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+
7685predicate small_handler_objectapi ( ExceptStmt ex , Stmt s , ClassObject type ) {
7786 not exists ( ex .getTry ( ) .getStmt ( 1 ) ) and
7887 s = ex .getTry ( ) .getStmt ( 0 ) and
7988 ex .getType ( ) .refersTo ( type )
8089}
8190
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+
8297/** Holds if this exception handler is sufficiently small in scope to not need a comment
8398 * as to what it is doing.
8499 */
@@ -95,12 +110,25 @@ predicate focussed_handler_objectapi(ExceptStmt ex) {
95110 )
96111}
97112
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+
98126Try try_return ( ) {
99127 not exists ( result .getStmt ( 1 ) ) and result .getStmt ( 0 ) instanceof Return
100128}
101129
102130from ExceptStmt ex
103131where empty_except ( ex ) and no_else ( ex ) and no_comment ( ex ) and not non_local_control_flow ( ex )
104132 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 )
106134select ex , "'except' clause does nothing but pass and there is no explanatory comment."
0 commit comments