| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 1 | def foo1(): |
| 2 | <weak_warning descr="Local variable 'aaa' value is not used">aaa</weak_warning> = 1 #fail |
| 3 | <weak_warning descr="Local variable 'aaa' value is not used">aaa</weak_warning> = 2 #fail |
| 4 | |
| 5 | bbb = 1 #pass |
| 6 | <weak_warning descr="Local variable 'bbb' value is not used">bbb</weak_warning> += 2 #fail |
| 7 | |
| 8 | def bar(<weak_warning descr="Parameter 'self' value is not used">self</weak_warning>): #fail |
| 9 | print("Foo") |
| 10 | |
| 11 | def baz(<weak_warning descr="Parameter 'a' value is not used">a</weak_warning>): #fail |
| 12 | a = 12 |
| 13 | print(a) |
| 14 | |
| 15 | def boo(): |
| 16 | <weak_warning descr="Local variable 'k' value is not used">k</weak_warning> = 1 #fail |
| 17 | [k for k in [1,3] if True] |
| 18 | |
| 19 | <weak_warning descr="Local variable 'i' value is not used">i</weak_warning> = 1 #fail |
| 20 | for i in [1,2]: |
| 21 | print(i) |
| 22 | |
| 23 | j = 1 #pass |
| 24 | for j in [-2, -1]: |
| 25 | print(j) |
| 26 | print (j) |
| 27 | |
| 28 | class A: |
| 29 | def foo(self): #pass |
| 30 | pass |
| 31 | |
| 32 | def baa(): |
| 33 | foo.bar = 123 #pass |
| 34 | |
| 35 | def bla(x): #pass |
| 36 | def _bar(): |
| 37 | print x |
| 38 | return _bar |
| 39 | |
| 40 | def main(): |
| 41 | foo = "foo" #pass |
| 42 | bar = "bar" #pass |
| 43 | baz = "baz" #pass |
| 44 | print "%(foo)s=%(bar)s" % locals() |
| 45 | |
| 46 | |
| 47 | def bar(arg): #pass |
| 48 | foo = 1 #pass |
| 49 | def <weak_warning descr="Local function 'test' is not used">test</weak_warning>(): #fail |
| 50 | print arg |
| 51 | return foo #pass |
| 52 | |
| 53 | class FooBar: |
| 54 | @classmethod |
| 55 | def foo(cls): #pass |
| 56 | pass |
| 57 | args[0] = 123 # pass |
| 58 | |
| 59 | def bzzz(): |
| 60 | for _ in xrange(1000): # pass |
| 61 | pass |
| 62 | |
| 63 | def do_something(local_var): |
| 64 | global global_var |
| 65 | global_var = local_var # pass |
| 66 | |
| 67 | def srange(n): |
| 68 | return {i for i in range(n)} |
| 69 | |
| 70 | def test_func(): |
| 71 | return {(lambda i=py1208: i) for py1208 in range(5)} |
| 72 | |
| 73 | |
| 74 | def foo(): |
| 75 | status = None #pass |
| 76 | try: |
| 77 | status = open('/proc/self/status', 'r') |
| 78 | finally: |
| 79 | if status is not None: |
| 80 | status.close() |
| 81 | |
| 82 | foo = lambda <weak_warning descr="Parameter 'x' value is not used">x</weak_warning>: False |
| 83 | |
| 84 | |
| 85 | class MySuperClass: |
| 86 | def shazam(self, param1, param2): pass |
| 87 | |
| 88 | class MySubClass(MySuperClass): |
| 89 | def shazam(self, param1, param2): pass |
| 90 | |
| 91 | def test_func(): |
| 92 | items = {(lambda i=i: i) for i in range(5)} #pass |
| 93 | return {x() for x in items} |
| 94 | |
| 95 | class MyType(type): |
| 96 | def __new__(cls, name, bases, attrs): #pass |
| 97 | if name.startswith('None'): |
| 98 | return None |
| 99 | return super(MyType, cls).__new__(cls, name, bases, newattrs) |
| 100 | |
| 101 | def foo(cls): #fail |
| 102 | pass |
| 103 | |
| 104 | def test(): |
| 105 | d = dict() |
| 106 | v = 1 #pass |
| 107 | try: |
| 108 | v = d['key'] |
| 109 | except KeyError: |
| 110 | v = 2 |
| 111 | finally: |
| 112 | print v |
| 113 | |
| 114 | def foo(<weak_warning descr="Parameter 'a' value is not used">a = 123</weak_warning>): # fail Do not use getText() as parameter name |
| 115 | print('hello') |
| 116 | |
| 117 | def loopie(): |
| 118 | for x in range(5): pass |
| 119 | for y in xrange(10): pass |
| 120 | |
| 121 | def locals_inside(): |
| 122 | now = datetime.datetime.now() # pass |
| 123 | do_smth_with(locals()) |
| 124 | |
| 125 | class Stat(object): |
| 126 | @staticmethod |
| 127 | def woof(<weak_warning descr="Parameter 'dog' value is not used">dog="bark"</weak_warning>): # fail |
| 128 | print('hello') |
| 129 | |
| 130 | class A: |
| 131 | def __init__(self, *args): #pass |
| 132 | self.args = [] |
| 133 | for a in args: |
| 134 | self.args.append(a) |
| 135 | |
| 136 | |
| 137 | # PY-2574 |
| 138 | def f(): |
| 139 | # x in "for" is actually used in "if" |
| 140 | return [0 for x in range(10) if x] #pass |
| 141 | |
| 142 | |
| 143 | # PY-3031 |
| 144 | def f(): |
| 145 | # n doesn't leak from the generator expression, so this n is used |
| 146 | n = 1 #pass |
| 147 | expr = (n for n in xrange(10)) |
| 148 | print n |
| 149 | return expr |
| 150 | |
| 151 | |
| 152 | # PY-3118 |
| 153 | def f(x): |
| 154 | # Both y values in "if" can be used later in g, so the first one shouldn't be marked as unused |
| 155 | if x: |
| 156 | y = 0 #pass |
| 157 | else: |
| 158 | y = 1 #pass |
| 159 | def g(): |
| 160 | return y |
| 161 | return g |
| 162 | |
| 163 | |
| 164 | # PY-3076 |
| 165 | def f(): |
| 166 | # Both x values could be used inside g (because there is no inter-procedural CFG) |
| 167 | x = 'foo' #pass |
| 168 | x = 'bar' #pass |
| 169 | def g(): |
| 170 | return x |
| 171 | x = 'baz' #pass |
| 172 | return g |
| 173 | |
| 174 | |
| 175 | # PY-2418 |
| 176 | def f(x): |
| 177 | # The list shouldn't be marked as unused |
| 178 | if isinstance(x, [tuple, list]): #pass |
| 179 | pass |
| 180 | |
| 181 | |
| 182 | # PY-3550 |
| 183 | def f(): |
| 184 | z = 2 #pass |
| 185 | def g(z=z): |
| 186 | return z |
| 187 | return g |
| 188 | |
| 189 | |
| 190 | # PY-4151 |
| 191 | def f(g): |
| 192 | x = 1 #pass |
| 193 | try: |
| 194 | x = g() |
| 195 | except Exception: |
| 196 | pass |
| 197 | else: |
| 198 | pass |
| 199 | print(x) |
| 200 | |
| 201 | |
| 202 | # PY-4154 |
| 203 | def a1(): |
| 204 | <weak_warning descr="Local variable 'a' value is not used">a</weak_warning> = 1 #fail |
| 205 | try: |
| 206 | a = 2 |
| 207 | except Exception: |
| 208 | a = 3 |
| 209 | print(a) |
| 210 | |
| 211 | |
| 212 | # PY-4157 |
| 213 | def f(g): |
| 214 | x = 1 #pass |
| 215 | try: |
| 216 | pass |
| 217 | except Exception: |
| 218 | pass |
| 219 | else: |
| 220 | x = g() |
| 221 | print(x) |
| 222 | |
| 223 | |
| 224 | # PY-4147 |
| 225 | def f(x, y, z): |
| 226 | class C: |
| 227 | foo = x #pass |
| 228 | def h(self): |
| 229 | return z |
| 230 | def g(): |
| 231 | return y |
| 232 | return C, g |
| 233 | |
| 234 | |
| 235 | # PY-4378 |
| 236 | def f(c): |
| 237 | try: |
| 238 | x = c['foo'] |
| 239 | except KeyError: |
| 240 | if c: |
| 241 | x = 42 #pass |
| 242 | else: |
| 243 | raise |
| 244 | except Exception: |
| 245 | raise |
| 246 | return x |
| 247 | |
| 248 | |
| 249 | # PY-5755 |
| 250 | def test_same_named_variable_inside_class(): |
| 251 | a = 1 #pass |
| 252 | class C: |
| 253 | def a(self): |
| 254 | print(a) |
| 255 | return C |
| 256 | |
| 257 | |
| 258 | # PY-5086 |
| 259 | def test_only_name_in_local_class(): |
| 260 | x = 1 |
| 261 | class <weak_warning descr="Local class 'C' is not used">C</weak_warning>: |
| 262 | pass |
| 263 | return x |
| 264 | |
| 265 | |
| 266 | # PY-7028 |
| 267 | class C: |
| 268 | def test_unused_params_in_empty_method_1(self, x, y, z): |
| 269 | pass |
| 270 | |
| 271 | def test_unused_params_in_empty_method_2(self, x, y, z): |
| 272 | raise Exception() |
| 273 | |
| 274 | def test_unused_params_in_empty_method_3(self, x, y, z): |
| 275 | """Docstring.""" |
| 276 | |
| 277 | def test_unused_params_in_empty_method_4(self, x, y, z): |
| 278 | """Docstring.""" |
| 279 | raise Exception() |
| 280 | |
| 281 | def test_unused_params_in_empty_method_5(self, x, y, z): |
| 282 | """Docstring.""" |
| 283 | return |
| 284 | |
| 285 | |
| 286 | # PY-7126 |
| 287 | def test_unused_empty_function(<weak_warning descr="Parameter 'x' value is not used">x</weak_warning>): |
| 288 | pass |
| 289 | |
| 290 | |
| 291 | # PY-7072 |
| 292 | def test_unused_variable_in_cycle(x, c): |
| 293 | while x > 0: |
| 294 | x -= 1 #pass |
| 295 | if c: |
| 296 | break |
| 297 | |
| 298 | |
| 299 | # PY-7517 |
| 300 | def test_unused_condition_local_with_last_if_in_cycle(c): |
| 301 | x = True |
| 302 | while x: |
| 303 | x = False #pass |
| 304 | if c: |
| 305 | x = True |
| Tor Norbye | 814f829 | 2014-03-06 17:27:18 -0800 | [diff] [blame^] | 306 | |
| 307 | |
| 308 | # PY-7527 |
| 309 | def test_unused_empty_init_parameter(): |
| 310 | class C(object): |
| 311 | def __init__(self, <weak_warning descr="Parameter 'foo' value is not used">foo</weak_warning>): |
| 312 | pass |
| 313 | |
| 314 | def f(self, bar): |
| 315 | pass |
| 316 | |
| 317 | return C |
| 318 | |