| 1 | // RUN: %check_clang_tidy %s bugprone-redundant-branch-condition %t \ |
| 2 | // RUN: -- -- -fblocks |
| 3 | |
| 4 | extern unsigned peopleInTheBuilding; |
| 5 | extern unsigned fireFighters; |
| 6 | |
| 7 | bool isBurning(); |
| 8 | bool isReallyBurning(); |
| 9 | bool isCollapsing(); |
| 10 | bool tryToExtinguish(bool&); |
| 11 | bool tryToExtinguishByVal(bool); |
| 12 | void tryPutFireOut(); |
| 13 | bool callTheFD(); |
| 14 | void scream(); |
| 15 | |
| 16 | bool someOtherCondition(); |
| 17 | |
| 18 | //===--- Basic Positives --------------------------------------------------===// |
| 19 | |
| 20 | void positive_direct() { |
| 21 | bool onFire = isBurning(); |
| 22 | if (onFire) { |
| 23 | if (onFire) { |
| 24 | // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition] |
| 25 | // CHECK-FIXES: {{^\ *$}} |
| 26 | scream(); |
| 27 | } |
| 28 | // CHECK-FIXES: {{^\ *$}} |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | void positive_indirect() { |
| 33 | bool onFire = isBurning(); |
| 34 | if (onFire) { |
| 35 | if (someOtherCondition()) { |
| 36 | if (onFire) |
| 37 | // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition] |
| 38 | // CHECK-FIXES: {{^\ *$}} |
| 39 | scream(); |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | void positive_direct_inner_and_lhs() { |
| 45 | bool onFire = isBurning(); |
| 46 | if (onFire) { |
| 47 | if (onFire && peopleInTheBuilding > 0) { |
| 48 | // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition] |
| 49 | // CHECK-FIXES: if ( peopleInTheBuilding > 0) { |
| 50 | scream(); |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | void positive_indirect_inner_and_lhs() { |
| 56 | bool onFire = isBurning(); |
| 57 | if (onFire) { |
| 58 | if (someOtherCondition()) { |
| 59 | if (onFire && peopleInTheBuilding > 0) { |
| 60 | // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition] |
| 61 | // CHECK-FIXES: if ( peopleInTheBuilding > 0) { |
| 62 | scream(); |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | void positive_direct_inner_and_rhs() { |
| 69 | bool onFire = isBurning(); |
| 70 | if (onFire) { |
| 71 | if (peopleInTheBuilding > 0 && onFire) { |
| 72 | // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition] |
| 73 | // CHECK-FIXES: if (peopleInTheBuilding > 0 ) { |
| 74 | scream(); |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | void positive_indirect_inner_and_rhs() { |
| 80 | bool onFire = isBurning(); |
| 81 | if (onFire) { |
| 82 | if (someOtherCondition()) { |
| 83 | if (peopleInTheBuilding > 0 && onFire) { |
| 84 | // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition] |
| 85 | // CHECK-FIXES: if (peopleInTheBuilding > 0 ) { |
| 86 | scream(); |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | void positive_direct_inner_or_lhs() { |
| 93 | bool onFire = isBurning(); |
| 94 | if (onFire) { |
| 95 | if (onFire || isCollapsing()) { |
| 96 | // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition] |
| 97 | // CHECK-FIXES: {{^\ *$}} |
| 98 | scream(); |
| 99 | } |
| 100 | // CHECK-FIXES: {{^\ *$}} |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | void positive_indirect_inner_or_lhs() { |
| 105 | bool onFire = isBurning(); |
| 106 | if (onFire) { |
| 107 | if (someOtherCondition()) { |
| 108 | if (onFire || isCollapsing()) { |
| 109 | // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition] |
| 110 | // CHECK-FIXES: {{^\ *$}} |
| 111 | scream(); |
| 112 | } |
| 113 | // CHECK-FIXES: {{^\ *$}} |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | void positive_direct_inner_or_rhs() { |
| 119 | bool onFire = isBurning(); |
| 120 | if (onFire) { |
| 121 | if (isCollapsing() || onFire) { |
| 122 | // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition] |
| 123 | // CHECK-FIXES: {{^\ *$}} |
| 124 | scream(); |
| 125 | } |
| 126 | // CHECK-FIXES: {{^\ *$}} |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | void positive_indirect_inner_or_rhs() { |
| 131 | bool onFire = isBurning(); |
| 132 | if (onFire) { |
| 133 | if (someOtherCondition()) { |
| 134 | if (isCollapsing() || onFire) { |
| 135 | // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition] |
| 136 | // CHECK-FIXES: {{^\ *$}} |
| 137 | scream(); |
| 138 | } |
| 139 | // CHECK-FIXES: {{^\ *$}} |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | void positive_direct_outer_and_lhs() { |
| 145 | bool onFire = isBurning(); |
| 146 | if (onFire && fireFighters < 10) { |
| 147 | if (onFire) { |
| 148 | // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition] |
| 149 | // CHECK-FIXES: {{^\ *$}} |
| 150 | scream(); |
| 151 | } |
| 152 | // CHECK-FIXES: {{^\ *$}} |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | void positive_indirect_outer_and_lhs() { |
| 157 | bool onFire = isBurning(); |
| 158 | if (onFire && fireFighters < 10) { |
| 159 | if (someOtherCondition()) { |
| 160 | if (onFire) { |
| 161 | // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition] |
| 162 | // CHECK-FIXES: {{^\ *$}} |
| 163 | scream(); |
| 164 | } |
| 165 | // CHECK-FIXES: {{^\ *$}} |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | void positive_direct_outer_and_lhs_inner_and_lhs() { |
| 171 | bool onFire = isBurning(); |
| 172 | if (onFire && fireFighters < 10) { |
| 173 | if (onFire && peopleInTheBuilding > 0) { |
| 174 | // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition] |
| 175 | // CHECK-FIXES: if ( peopleInTheBuilding > 0) { |
| 176 | scream(); |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | void positive_indirect_outer_and_lhs_inner_and_lhs() { |
| 182 | bool onFire = isBurning(); |
| 183 | if (onFire && fireFighters < 10) { |
| 184 | if (someOtherCondition()) { |
| 185 | if (onFire && peopleInTheBuilding > 0) { |
| 186 | // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition] |
| 187 | // CHECK-FIXES: if ( peopleInTheBuilding > 0) { |
| 188 | scream(); |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | void positive_direct_outer_and_lhs_inner_and_rhs() { |
| 195 | bool onFire = isBurning(); |
| 196 | if (onFire && fireFighters < 10) { |
| 197 | if (peopleInTheBuilding > 0 && onFire) { |
| 198 | // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition] |
| 199 | // CHECK-FIXES: if (peopleInTheBuilding > 0 ) { |
| 200 | scream(); |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | void positive_indirect_outer_and_lhs_inner_and_rhs() { |
| 206 | bool onFire = isBurning(); |
| 207 | if (onFire && fireFighters < 10) { |
| 208 | if (someOtherCondition()) { |
| 209 | if (peopleInTheBuilding > 0 && onFire) { |
| 210 | // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition] |
| 211 | // CHECK-FIXES: if (peopleInTheBuilding > 0 ) { |
| 212 | scream(); |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | void positive_direct_outer_and_lhs_inner_or_lhs() { |
| 219 | bool onFire = isBurning(); |
| 220 | if (onFire && fireFighters < 10) { |
| 221 | if (onFire || isCollapsing()) { |
| 222 | // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition] |
| 223 | // CHECK-FIXES: {{^\ *$}} |
| 224 | scream(); |
| 225 | } |
| 226 | // CHECK-FIXES: {{^\ *$}} |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | void positive_indirect_outer_and_lhs_inner_or_lhs() { |
| 231 | bool onFire = isBurning(); |
| 232 | if (onFire && fireFighters < 10) { |
| 233 | if (someOtherCondition()) { |
| 234 | if (onFire || isCollapsing()) { |
| 235 | // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition] |
| 236 | // CHECK-FIXES: {{^\ *$}} |
| 237 | scream(); |
| 238 | } |
| 239 | // CHECK-FIXES: {{^\ *$}} |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | void positive_direct_outer_and_lhs_inner_or_rhs() { |
| 245 | bool onFire = isBurning(); |
| 246 | if (onFire && fireFighters < 10) { |
| 247 | if (isCollapsing() || onFire) { |
| 248 | // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition] |
| 249 | // CHECK-FIXES: {{^\ *$}} |
| 250 | scream(); |
| 251 | } |
| 252 | // CHECK-FIXES: {{^\ *$}} |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | void positive_indirect_outer_and_lhs_inner_or_rhs() { |
| 257 | bool onFire = isBurning(); |
| 258 | if (onFire && fireFighters < 10) { |
| 259 | if (someOtherCondition()) { |
| 260 | if (isCollapsing() || onFire) { |
| 261 | // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition] |
| 262 | // CHECK-FIXES: {{^\ *$}} |
| 263 | scream(); |
| 264 | } |
| 265 | // CHECK-FIXES: {{^\ *$}} |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | void positive_direct_outer_and_rhs() { |
| 271 | bool onFire = isBurning(); |
| 272 | if (fireFighters < 10 && onFire) { |
| 273 | if (onFire) { |
| 274 | // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition] |
| 275 | // CHECK-FIXES: {{^\ *$}} |
| 276 | scream(); |
| 277 | } |
| 278 | // CHECK-FIXES: {{^\ *$}} |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | void positive_indirect_outer_and_rhs() { |
| 283 | bool onFire = isBurning(); |
| 284 | if (fireFighters < 10 && onFire) { |
| 285 | if (someOtherCondition()) { |
| 286 | if (onFire) { |
| 287 | // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition] |
| 288 | // CHECK-FIXES: {{^\ *$}} |
| 289 | scream(); |
| 290 | } |
| 291 | // CHECK-FIXES: {{^\ *$}} |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | void positive_direct_outer_and_rhs_inner_and_lhs() { |
| 297 | bool onFire = isBurning(); |
| 298 | if (fireFighters < 10 && onFire) { |
| 299 | if (onFire && peopleInTheBuilding > 0) { |
| 300 | // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition] |
| 301 | // CHECK-FIXES: if ( peopleInTheBuilding > 0) { |
| 302 | scream(); |
| 303 | } |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | void positive_indirect_outer_and_rhs_inner_and_lhs() { |
| 308 | bool onFire = isBurning(); |
| 309 | if (fireFighters < 10 && onFire) { |
| 310 | if (someOtherCondition()) { |
| 311 | if (onFire && peopleInTheBuilding > 0) { |
| 312 | // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition] |
| 313 | // CHECK-FIXES: if ( peopleInTheBuilding > 0) { |
| 314 | scream(); |
| 315 | } |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | void positive_direct_inner_outer_and_rhs_and_rhs() { |
| 321 | bool onFire = isBurning(); |
| 322 | if (fireFighters < 10 && onFire) { |
| 323 | if (peopleInTheBuilding > 0 && onFire) { |
| 324 | // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition] |
| 325 | // CHECK-FIXES: if (peopleInTheBuilding > 0 ) { |
| 326 | scream(); |
| 327 | } |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | void positive_indirect_outer_and_rhs_inner_and_rhs() { |
| 332 | bool onFire = isBurning(); |
| 333 | if (fireFighters < 10 && onFire) { |
| 334 | if (someOtherCondition()) { |
| 335 | if (peopleInTheBuilding > 0 && onFire) { |
| 336 | // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition] |
| 337 | // CHECK-FIXES: if (peopleInTheBuilding > 0 ) { |
| 338 | scream(); |
| 339 | } |
| 340 | } |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | void positive_direct_outer_and_rhs_inner_or_lhs() { |
| 345 | bool onFire = isBurning(); |
| 346 | if (fireFighters < 10 && onFire) { |
| 347 | if (onFire || isCollapsing()) { |
| 348 | // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition] |
| 349 | // CHECK-FIXES: {{^\ *$}} |
| 350 | scream(); |
| 351 | } |
| 352 | // CHECK-FIXES: {{^\ *$}} |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | void positive_indirect_outer_and_rhs_inner_or_lhs() { |
| 357 | bool onFire = isBurning(); |
| 358 | if (fireFighters < 10 && onFire) { |
| 359 | if (someOtherCondition()) { |
| 360 | if (onFire || isCollapsing()) { |
| 361 | // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition] |
| 362 | // CHECK-FIXES: {{^\ *$}} |
| 363 | scream(); |
| 364 | } |
| 365 | // CHECK-FIXES: {{^\ *$}} |
| 366 | } |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | void positive_direct_outer_and_rhs_inner_or_rhs() { |
| 371 | bool onFire = isBurning(); |
| 372 | if (fireFighters < 10 && onFire) { |
| 373 | if (isCollapsing() || onFire) { |
| 374 | // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition] |
| 375 | // CHECK-FIXES: {{^\ *$}} |
| 376 | scream(); |
| 377 | } |
| 378 | // CHECK-FIXES: {{^\ *$}} |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | void positive_indirect_outer_and_rhs_inner_or_rhs() { |
| 383 | bool onFire = isBurning(); |
| 384 | if (fireFighters < 10 && onFire) { |
| 385 | if (someOtherCondition()) { |
| 386 | if (isCollapsing() || onFire) { |
| 387 | // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition] |
| 388 | // CHECK-FIXES: {{^\ *$}} |
| 389 | scream(); |
| 390 | } |
| 391 | // CHECK-FIXES: {{^\ *$}} |
| 392 | } |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | //===--- Basic Negatives --------------------------------------------------===// |
| 397 | |
| 398 | void negative_direct() { |
| 399 | bool onFire = isBurning(); |
| 400 | if (onFire) { |
| 401 | tryToExtinguish(onFire); |
| 402 | if (onFire) { |
| 403 | // NO-MESSAGE: fire may have been extinguished |
| 404 | scream(); |
| 405 | } |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | void negative_indirect() { |
| 410 | bool onFire = isBurning(); |
| 411 | if (onFire) { |
| 412 | tryToExtinguish(onFire); |
| 413 | if (someOtherCondition()) { |
| 414 | if (onFire) { |
| 415 | // NO-MESSAGE: fire may have been extinguished |
| 416 | scream(); |
| 417 | } |
| 418 | } |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | void negative_indirect2() { |
| 423 | bool onFire = isBurning(); |
| 424 | if (onFire) { |
| 425 | if (someOtherCondition()) { |
| 426 | tryToExtinguish(onFire); |
| 427 | if (onFire) { |
| 428 | // NO-MESSAGE: fire may have been extinguished |
| 429 | scream(); |
| 430 | } |
| 431 | } |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | void negative_direct_inner_and_lhs() { |
| 436 | bool onFire = isBurning(); |
| 437 | if (onFire) { |
| 438 | tryToExtinguish(onFire); |
| 439 | if (onFire && peopleInTheBuilding > 0) { |
| 440 | // NO-MESSAGE: fire may have been extinguished |
| 441 | scream(); |
| 442 | } |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | void negative_indirect_inner_and_lhs() { |
| 447 | bool onFire = isBurning(); |
| 448 | if (onFire) { |
| 449 | tryToExtinguish(onFire); |
| 450 | if (someOtherCondition()) { |
| 451 | if (onFire && peopleInTheBuilding > 0) { |
| 452 | // NO-MESSAGE: fire may have been extinguished |
| 453 | scream(); |
| 454 | } |
| 455 | } |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | void negative_indirect2_inner_and_lhs() { |
| 460 | bool onFire = isBurning(); |
| 461 | if (onFire) { |
| 462 | if (someOtherCondition()) { |
| 463 | tryToExtinguish(onFire); |
| 464 | if (onFire && peopleInTheBuilding > 0) { |
| 465 | // NO-MESSAGE: fire may have been extinguished |
| 466 | scream(); |
| 467 | } |
| 468 | } |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | void negative_direct_inner_and_rhs() { |
| 473 | bool onFire = isBurning(); |
| 474 | if (onFire) { |
| 475 | tryToExtinguish(onFire); |
| 476 | if (peopleInTheBuilding > 0 && onFire) { |
| 477 | // NO-MESSAGE: fire may have been extinguished |
| 478 | scream(); |
| 479 | } |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | void negative_indirect_inner_and_rhs() { |
| 484 | bool onFire = isBurning(); |
| 485 | if (onFire) { |
| 486 | tryToExtinguish(onFire); |
| 487 | if (someOtherCondition()) { |
| 488 | if (peopleInTheBuilding > 0 && onFire) { |
| 489 | // NO-MESSAGE: fire may have been extinguished |
| 490 | scream(); |
| 491 | } |
| 492 | } |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | void negative_indirect2_inner_and_rhs() { |
| 497 | bool onFire = isBurning(); |
| 498 | if (onFire) { |
| 499 | if (someOtherCondition()) { |
| 500 | tryToExtinguish(onFire); |
| 501 | if (peopleInTheBuilding > 0 && onFire) { |
| 502 | // NO-MESSAGE: fire may have been extinguished |
| 503 | scream(); |
| 504 | } |
| 505 | } |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | void negative_direct_inner_or_lhs() { |
| 510 | bool onFire = isBurning(); |
| 511 | if (onFire) { |
| 512 | tryToExtinguish(onFire); |
| 513 | if (onFire || isCollapsing()) { |
| 514 | // NO-MESSAGE: fire may have been extinguished |
| 515 | scream(); |
| 516 | } |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | void negative_indirect_inner_or_lhs() { |
| 521 | bool onFire = isBurning(); |
| 522 | if (onFire) { |
| 523 | tryToExtinguish(onFire); |
| 524 | if (someOtherCondition()) { |
| 525 | if (onFire || isCollapsing()) { |
| 526 | // NO-MESSAGE: fire may have been extinguished |
| 527 | scream(); |
| 528 | } |
| 529 | } |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | void negative_indirect2_inner_or_lhs() { |
| 534 | bool onFire = isBurning(); |
| 535 | if (onFire) { |
| 536 | if (someOtherCondition()) { |
| 537 | tryToExtinguish(onFire); |
| 538 | if (onFire || isCollapsing()) { |
| 539 | // NO-MESSAGE: fire may have been extinguished |
| 540 | scream(); |
| 541 | } |
| 542 | } |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | void negative_direct_inner_or_rhs() { |
| 547 | bool onFire = isBurning(); |
| 548 | if (onFire) { |
| 549 | tryToExtinguish(onFire); |
| 550 | if (isCollapsing() || onFire) { |
| 551 | // NO-MESSAGE: fire may have been extinguished |
| 552 | scream(); |
| 553 | } |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | void negative_indirect_inner_or_rhs() { |
| 558 | bool onFire = isBurning(); |
| 559 | if (onFire) { |
| 560 | tryToExtinguish(onFire); |
| 561 | if (someOtherCondition()) { |
| 562 | if (isCollapsing() || onFire) { |
| 563 | // NO-MESSAGE: fire may have been extinguished |
| 564 | scream(); |
| 565 | } |
| 566 | } |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | void negative_indirect2_inner_or_rhs() { |
| 571 | bool onFire = isBurning(); |
| 572 | if (onFire) { |
| 573 | if (someOtherCondition()) { |
| 574 | tryToExtinguish(onFire); |
| 575 | if (isCollapsing() || onFire) { |
| 576 | // NO-MESSAGE: fire may have been extinguished |
| 577 | scream(); |
| 578 | } |
| 579 | } |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | void negative_direct_outer_and_lhs() { |
| 584 | bool onFire = isBurning(); |
| 585 | if (onFire && fireFighters < 10) { |
| 586 | tryToExtinguish(onFire); |
| 587 | if (onFire) { |
| 588 | // NO-MESSAGE: fire may have been extinguished |
| 589 | scream(); |
| 590 | } |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | void negative_indirect_outer_and_lhs() { |
| 595 | bool onFire = isBurning(); |
| 596 | if (onFire && fireFighters < 10) { |
| 597 | tryToExtinguish(onFire); |
| 598 | if (someOtherCondition()) { |
| 599 | if (onFire) { |
| 600 | // NO-MESSAGE: fire may have been extinguished |
| 601 | scream(); |
| 602 | } |
| 603 | } |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | void negative_indirect2_outer_and_lhs() { |
| 608 | bool onFire = isBurning(); |
| 609 | if (onFire && fireFighters < 10) { |
| 610 | if (someOtherCondition()) { |
| 611 | tryToExtinguish(onFire); |
| 612 | if (onFire) { |
| 613 | // NO-MESSAGE: fire may have been extinguished |
| 614 | scream(); |
| 615 | } |
| 616 | } |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | void negative_direct_outer_and_lhs_inner_and_lhs() { |
| 621 | bool onFire = isBurning(); |
| 622 | if (onFire && fireFighters < 10) { |
| 623 | tryToExtinguish(onFire); |
| 624 | if (onFire && peopleInTheBuilding > 0) { |
| 625 | // NO-MESSAGE: fire may have been extinguished |
| 626 | scream(); |
| 627 | } |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | void negative_indirect_outer_and_lhs_inner_and_lhs() { |
| 632 | bool onFire = isBurning(); |
| 633 | if (onFire && fireFighters < 10) { |
| 634 | tryToExtinguish(onFire); |
| 635 | if (someOtherCondition()) { |
| 636 | if (onFire && peopleInTheBuilding > 0) { |
| 637 | // NO-MESSAGE: fire may have been extinguished |
| 638 | scream(); |
| 639 | } |
| 640 | } |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | void negative_indirect2_outer_and_lhs_inner_and_lhs() { |
| 645 | bool onFire = isBurning(); |
| 646 | if (onFire && fireFighters < 10) { |
| 647 | if (someOtherCondition()) { |
| 648 | tryToExtinguish(onFire); |
| 649 | if (onFire && peopleInTheBuilding > 0) { |
| 650 | // NO-MESSAGE: fire may have been extinguished |
| 651 | scream(); |
| 652 | } |
| 653 | } |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | void negative_direct_outer_and_lhs_inner_and_rhs() { |
| 658 | bool onFire = isBurning(); |
| 659 | if (onFire && fireFighters < 10) { |
| 660 | tryToExtinguish(onFire); |
| 661 | if (peopleInTheBuilding > 0 && onFire) { |
| 662 | // NO-MESSAGE: fire may have been extinguished |
| 663 | scream(); |
| 664 | } |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | void negative_indirect_outer_and_lhs_inner_and_rhs() { |
| 669 | bool onFire = isBurning(); |
| 670 | if (onFire && fireFighters < 10) { |
| 671 | tryToExtinguish(onFire); |
| 672 | if (someOtherCondition()) { |
| 673 | if (peopleInTheBuilding > 0 && onFire) { |
| 674 | // NO-MESSAGE: fire may have been extinguished |
| 675 | scream(); |
| 676 | } |
| 677 | } |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | void negative_indirect2_outer_and_lhs_inner_and_rhs() { |
| 682 | bool onFire = isBurning(); |
| 683 | if (onFire && fireFighters < 10) { |
| 684 | if (someOtherCondition()) { |
| 685 | tryToExtinguish(onFire); |
| 686 | if (peopleInTheBuilding > 0 && onFire) { |
| 687 | // NO-MESSAGE: fire may have been extinguished |
| 688 | scream(); |
| 689 | } |
| 690 | } |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | void negative_direct_outer_and_lhs_inner_or_lhs() { |
| 695 | bool onFire = isBurning(); |
| 696 | if (onFire && fireFighters < 10) { |
| 697 | tryToExtinguish(onFire); |
| 698 | if (onFire || isCollapsing()) { |
| 699 | // NO-MESSAGE: fire may have been extinguished |
| 700 | scream(); |
| 701 | } |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | void negative_indirect_outer_and_lhs_inner_or_lhs() { |
| 706 | bool onFire = isBurning(); |
| 707 | if (onFire && fireFighters < 10) { |
| 708 | tryToExtinguish(onFire); |
| 709 | if (someOtherCondition()) { |
| 710 | if (onFire || isCollapsing()) { |
| 711 | // NO-MESSAGE: fire may have been extinguished |
| 712 | scream(); |
| 713 | } |
| 714 | } |
| 715 | } |
| 716 | } |
| 717 | |
| 718 | void negative_indirect2_outer_and_lhs_inner_or_lhs() { |
| 719 | bool onFire = isBurning(); |
| 720 | if (onFire && fireFighters < 10) { |
| 721 | if (someOtherCondition()) { |
| 722 | tryToExtinguish(onFire); |
| 723 | if (onFire || isCollapsing()) { |
| 724 | // NO-MESSAGE: fire may have been extinguished |
| 725 | scream(); |
| 726 | } |
| 727 | } |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | void negative_direct_outer_and_lhs_inner_or_rhs() { |
| 732 | bool onFire = isBurning(); |
| 733 | if (onFire && fireFighters < 10) { |
| 734 | tryToExtinguish(onFire); |
| 735 | if (isCollapsing() || onFire) { |
| 736 | // NO-MESSAGE: fire may have been extinguished |
| 737 | scream(); |
| 738 | } |
| 739 | } |
| 740 | } |
| 741 | |
| 742 | void negative_indirect_outer_and_lhs_inner_or_rhs() { |
| 743 | bool onFire = isBurning(); |
| 744 | if (onFire && fireFighters < 10) { |
| 745 | tryToExtinguish(onFire); |
| 746 | if (someOtherCondition()) { |
| 747 | if (isCollapsing() || onFire) { |
| 748 | // NO-MESSAGE: fire may have been extinguished |
| 749 | scream(); |
| 750 | } |
| 751 | } |
| 752 | } |
| 753 | } |
| 754 | |
| 755 | void negative_indirect2_outer_and_lhs_inner_or_rhs() { |
| 756 | bool onFire = isBurning(); |
| 757 | if (onFire && fireFighters < 10) { |
| 758 | if (someOtherCondition()) { |
| 759 | tryToExtinguish(onFire); |
| 760 | if (isCollapsing() || onFire) { |
| 761 | // NO-MESSAGE: fire may have been extinguished |
| 762 | scream(); |
| 763 | } |
| 764 | } |
| 765 | } |
| 766 | } |
| 767 | |
| 768 | void negative_direct_outer_and_rhs() { |
| 769 | bool onFire = isBurning(); |
| 770 | if (fireFighters < 10 && onFire) { |
| 771 | tryToExtinguish(onFire); |
| 772 | if (onFire) { |
| 773 | // NO-MESSAGE: fire may have been extinguished |
| 774 | scream(); |
| 775 | } |
| 776 | } |
| 777 | } |
| 778 | |
| 779 | void negative_indirect_outer_and_rhs() { |
| 780 | bool onFire = isBurning(); |
| 781 | if (fireFighters < 10 && onFire) { |
| 782 | tryToExtinguish(onFire); |
| 783 | if (someOtherCondition()) { |
| 784 | if (onFire) { |
| 785 | // NO-MESSAGE: fire may have been extinguished |
| 786 | scream(); |
| 787 | } |
| 788 | } |
| 789 | } |
| 790 | } |
| 791 | |
| 792 | void negative_indirect2_outer_and_rhs() { |
| 793 | bool onFire = isBurning(); |
| 794 | if (fireFighters < 10 && onFire) { |
| 795 | if (someOtherCondition()) { |
| 796 | tryToExtinguish(onFire); |
| 797 | if (onFire) { |
| 798 | // NO-MESSAGE: fire may have been extinguished |
| 799 | scream(); |
| 800 | } |
| 801 | } |
| 802 | } |
| 803 | } |
| 804 | |
| 805 | void negative_direct_outer_and_rhs_inner_and_lhs() { |
| 806 | bool onFire = isBurning(); |
| 807 | if (fireFighters < 10 && onFire) { |
| 808 | tryToExtinguish(onFire); |
| 809 | if (onFire && peopleInTheBuilding > 0) { |
| 810 | // NO-MESSAGE: fire may have been extinguished |
| 811 | scream(); |
| 812 | } |
| 813 | } |
| 814 | } |
| 815 | |
| 816 | void negative_indirect_outer_and_rhs_inner_and_lhs() { |
| 817 | bool onFire = isBurning(); |
| 818 | if (fireFighters < 10 && onFire) { |
| 819 | tryToExtinguish(onFire); |
| 820 | if (someOtherCondition()) { |
| 821 | if (onFire && peopleInTheBuilding > 0) { |
| 822 | // NO-MESSAGE: fire may have been extinguished |
| 823 | scream(); |
| 824 | } |
| 825 | } |
| 826 | } |
| 827 | } |
| 828 | |
| 829 | void negative_indirect2_outer_and_rhs_inner_and_lhs() { |
| 830 | bool onFire = isBurning(); |
| 831 | if (fireFighters < 10 && onFire) { |
| 832 | if (someOtherCondition()) { |
| 833 | tryToExtinguish(onFire); |
| 834 | if (onFire && peopleInTheBuilding > 0) { |
| 835 | // NO-MESSAGE: fire may have been extinguished |
| 836 | scream(); |
| 837 | } |
| 838 | } |
| 839 | } |
| 840 | } |
| 841 | |
| 842 | void negative_direct_inner_outer_and_rhs_and_rhs() { |
| 843 | bool onFire = isBurning(); |
| 844 | if (fireFighters < 10 && onFire) { |
| 845 | tryToExtinguish(onFire); |
| 846 | if (peopleInTheBuilding > 0 && onFire) { |
| 847 | // NO-MESSAGE: fire may have been extinguished |
| 848 | scream(); |
| 849 | } |
| 850 | } |
| 851 | } |
| 852 | |
| 853 | void negative_indirect_outer_and_rhs_inner_and_rhs() { |
| 854 | bool onFire = isBurning(); |
| 855 | if (fireFighters < 10 && onFire) { |
| 856 | tryToExtinguish(onFire); |
| 857 | if (someOtherCondition()) { |
| 858 | if (peopleInTheBuilding > 0 && onFire) { |
| 859 | // NO-MESSAGE: fire may have been extinguished |
| 860 | scream(); |
| 861 | } |
| 862 | } |
| 863 | } |
| 864 | } |
| 865 | |
| 866 | void negative_indirect2_outer_and_rhs_inner_and_rhs() { |
| 867 | bool onFire = isBurning(); |
| 868 | if (fireFighters < 10 && onFire) { |
| 869 | if (someOtherCondition()) { |
| 870 | tryToExtinguish(onFire); |
| 871 | if (peopleInTheBuilding > 0 && onFire) { |
| 872 | // NO-MESSAGE: fire may have been extinguished |
| 873 | scream(); |
| 874 | } |
| 875 | } |
| 876 | } |
| 877 | } |
| 878 | |
| 879 | void negative_direct_outer_and_rhs_inner_or_lhs() { |
| 880 | bool onFire = isBurning(); |
| 881 | if (fireFighters < 10 && onFire) { |
| 882 | tryToExtinguish(onFire); |
| 883 | if (onFire || isCollapsing()) { |
| 884 | // NO-MESSAGE: fire may have been extinguished |
| 885 | scream(); |
| 886 | } |
| 887 | } |
| 888 | } |
| 889 | |
| 890 | void negative_indirect_outer_and_rhs_inner_or_lhs() { |
| 891 | bool onFire = isBurning(); |
| 892 | if (fireFighters < 10 && onFire) { |
| 893 | tryToExtinguish(onFire); |
| 894 | if (someOtherCondition()) { |
| 895 | if (onFire || isCollapsing()) { |
| 896 | // NO-MESSAGE: fire may have been extinguished |
| 897 | scream(); |
| 898 | } |
| 899 | } |
| 900 | } |
| 901 | } |
| 902 | |
| 903 | void negative_indirect2_outer_and_rhs_inner_or_lhs() { |
| 904 | bool onFire = isBurning(); |
| 905 | if (fireFighters < 10 && onFire) { |
| 906 | if (someOtherCondition()) { |
| 907 | tryToExtinguish(onFire); |
| 908 | if (onFire || isCollapsing()) { |
| 909 | // NO-MESSAGE: fire may have been extinguished |
| 910 | scream(); |
| 911 | } |
| 912 | } |
| 913 | } |
| 914 | } |
| 915 | |
| 916 | void negative_direct_outer_and_rhs_inner_or_rhs() { |
| 917 | bool onFire = isBurning(); |
| 918 | if (fireFighters < 10 && onFire) { |
| 919 | tryToExtinguish(onFire); |
| 920 | if (isCollapsing() || onFire) { |
| 921 | // NO-MESSAGE: fire may have been extinguished |
| 922 | scream(); |
| 923 | } |
| 924 | } |
| 925 | } |
| 926 | |
| 927 | void negative_indirect_outer_and_rhs_inner_or_rhs() { |
| 928 | bool onFire = isBurning(); |
| 929 | if (fireFighters < 10 && onFire) { |
| 930 | tryToExtinguish(onFire); |
| 931 | if (someOtherCondition()) { |
| 932 | if (isCollapsing() || onFire) { |
| 933 | // NO-MESSAGE: fire may have been extinguished |
| 934 | scream(); |
| 935 | } |
| 936 | } |
| 937 | } |
| 938 | } |
| 939 | |
| 940 | void negative_indirect2_outer_and_rhs_inner_or_rhs() { |
| 941 | bool onFire = isBurning(); |
| 942 | if (fireFighters < 10 && onFire) { |
| 943 | if (someOtherCondition()) { |
| 944 | tryToExtinguish(onFire); |
| 945 | if (isCollapsing() || onFire) { |
| 946 | // NO-MESSAGE: fire may have been extinguished |
| 947 | scream(); |
| 948 | } |
| 949 | } |
| 950 | } |
| 951 | } |
| 952 | |
| 953 | void negative_by_ref(bool onFire) { |
| 954 | if (tryToExtinguish(onFire) && onFire) { |
| 955 | if (tryToExtinguish(onFire) && onFire) { |
| 956 | // NO-MESSAGE: fire may have been extinguished |
| 957 | scream(); |
| 958 | } |
| 959 | } |
| 960 | } |
| 961 | |
| 962 | void negative_by_val(bool onFire) { |
| 963 | if (tryToExtinguishByVal(onFire) && onFire) { |
| 964 | if (tryToExtinguish(onFire) && onFire) { |
| 965 | // NO-MESSAGE: fire may have been extinguished |
| 966 | scream(); |
| 967 | } |
| 968 | } |
| 969 | if (tryToExtinguish(onFire) && onFire) { |
| 970 | if (tryToExtinguishByVal(onFire) && onFire) { |
| 971 | // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition] |
| 972 | scream(); |
| 973 | } |
| 974 | } |
| 975 | } |
| 976 | |
| 977 | void negative_reassigned() { |
| 978 | bool onFire = isBurning(); |
| 979 | if (onFire) { |
| 980 | onFire = isReallyBurning(); |
| 981 | if (onFire) { |
| 982 | // NO-MESSAGE: it was a false alarm then |
| 983 | scream(); |
| 984 | } |
| 985 | } |
| 986 | } |
| 987 | |
| 988 | //===--- Special Positives ------------------------------------------------===// |
| 989 | |
| 990 | // Condition variable mutated in or after the inner loop |
| 991 | |
| 992 | void positive_direct_mutated_after_inner() { |
| 993 | bool onFire = isBurning(); |
| 994 | if (onFire) { |
| 995 | if (onFire) { |
| 996 | // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition] |
| 997 | // CHECK-FIXES: {{^\ *$}} |
| 998 | scream(); |
| 999 | } |
| 1000 | // CHECK-FIXES: {{^\ *$}} |
| 1001 | tryToExtinguish(onFire); |
| 1002 | } |
| 1003 | } |
| 1004 | |
| 1005 | void positive_indirect_mutated_after_inner() { |
| 1006 | bool onFire = isBurning(); |
| 1007 | if (onFire) { |
| 1008 | if (someOtherCondition()) { |
| 1009 | if (onFire) { |
| 1010 | // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition] |
| 1011 | // CHECK-FIXES: {{^\ *$}} |
| 1012 | scream(); |
| 1013 | } |
| 1014 | // CHECK-FIXES: {{^\ *$}} |
| 1015 | } |
| 1016 | tryToExtinguish(onFire); |
| 1017 | } |
| 1018 | } |
| 1019 | |
| 1020 | void positive_indirect2_mutated_after_inner() { |
| 1021 | bool onFire = isBurning(); |
| 1022 | if (onFire) { |
| 1023 | if (someOtherCondition()) { |
| 1024 | if (onFire) { |
| 1025 | // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition] |
| 1026 | // CHECK-FIXES: {{^\ *$}} |
| 1027 | scream(); |
| 1028 | } |
| 1029 | // CHECK-FIXES: {{^\ *$}} |
| 1030 | tryToExtinguish(onFire); |
| 1031 | } |
| 1032 | } |
| 1033 | } |
| 1034 | |
| 1035 | void positive_mutated_in_inner() { |
| 1036 | bool onFire = isBurning(); |
| 1037 | if (onFire) { |
| 1038 | if (onFire) { |
| 1039 | // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition] |
| 1040 | // CHECK-FIXES: {{^\ *$}} |
| 1041 | tryToExtinguish(onFire); |
| 1042 | scream(); |
| 1043 | } |
| 1044 | // CHECK-FIXES: {{^\ *$}} |
| 1045 | } |
| 1046 | } |
| 1047 | |
| 1048 | void positive_or_lhs_with_side_effect() { |
| 1049 | bool onFire = isBurning(); |
| 1050 | if (onFire) { |
| 1051 | if (callTheFD() || onFire) { |
| 1052 | // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition] |
| 1053 | // CHECK-FIXES: callTheFD() ; |
| 1054 | scream(); |
| 1055 | } |
| 1056 | // CHECK-FIXES: {{^\ *$}} |
| 1057 | } |
| 1058 | } |
| 1059 | |
| 1060 | void positive_or_rhs_with_side_effect() { |
| 1061 | bool onFire = isBurning(); |
| 1062 | if (onFire) { |
| 1063 | if (onFire || callTheFD()) { |
| 1064 | // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition] |
| 1065 | // CHECK-FIXES: callTheFD(); |
| 1066 | scream(); |
| 1067 | } |
| 1068 | // CHECK-FIXES: {{^\ *$}} |
| 1069 | } |
| 1070 | } |
| 1071 | |
| 1072 | // GNU Expression Statements |
| 1073 | |
| 1074 | void doSomething(); |
| 1075 | |
| 1076 | void positive_gnu_expression_statement() { |
| 1077 | bool onFire = isBurning(); |
| 1078 | if (({ doSomething(); onFire; })) { |
| 1079 | if (({ doSomething(); onFire; })) { |
| 1080 | // FIXME: Handle GNU epxression statements |
| 1081 | // CHECK-MESSAGES-NOT: :[[@LINE-1]]:5: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition] |
| 1082 | // CHCK-FIXES-NOT: doSomething(); |
| 1083 | scream(); |
| 1084 | } |
| 1085 | } |
| 1086 | } |
| 1087 | |
| 1088 | // Comma after Condition |
| 1089 | |
| 1090 | void positive_comma_after_condition() { |
| 1091 | bool onFire = isBurning(); |
| 1092 | if (doSomething(), onFire) { |
| 1093 | if (doSomething(), onFire) { |
| 1094 | // FIXME: Handle comma operator |
| 1095 | // CHECK-MESSAGES-NOT: :[[@LINE-1]]:5: warning: redundant condition 'onFire' [bugprone-redundant-branch-condition] |
| 1096 | // CHCK-FIXES-NOT: doSomething(); |
| 1097 | scream(); |
| 1098 | } |
| 1099 | } |
| 1100 | } |
| 1101 | |
| 1102 | // ExprWithCleanups doesn't crash |
| 1103 | int positive_expr_with_cleanups() { |
| 1104 | class RetT { |
| 1105 | public: |
| 1106 | RetT(const int code); |
| 1107 | bool Ok() const; |
| 1108 | static RetT Test(bool isSet); |
| 1109 | |
| 1110 | private: |
| 1111 | int code_; |
| 1112 | }; |
| 1113 | |
| 1114 | bool isSet = false; |
| 1115 | if (RetT::Test(isSet).Ok() && isSet) { |
| 1116 | if (RetT::Test(isSet).Ok() && isSet) { |
| 1117 | // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant condition 'isSet' [bugprone-redundant-branch-condition] |
| 1118 | // CHECK-FIXES: if (RetT::Test(isSet).Ok() ) { |
| 1119 | } |
| 1120 | } |
| 1121 | if (isSet) { |
| 1122 | if ((RetT::Test(isSet).Ok() && isSet)) { |
| 1123 | // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant condition 'isSet' [bugprone-redundant-branch-condition] |
| 1124 | // CHECK-FIXES: if ((RetT::Test(isSet).Ok() )) { |
| 1125 | } |
| 1126 | } |
| 1127 | return 0; |
| 1128 | } |
| 1129 | |
| 1130 | //===--- Special Negatives ------------------------------------------------===// |
| 1131 | |
| 1132 | // Aliasing |
| 1133 | |
| 1134 | void negative_mutated_by_ptr() { |
| 1135 | bool onFire = isBurning(); |
| 1136 | bool *firePtr = &onFire; |
| 1137 | if (onFire) { |
| 1138 | tryToExtinguish(*firePtr); |
| 1139 | if (onFire) { |
| 1140 | // NO-MESSAGE: fire may have been extinguished |
| 1141 | scream(); |
| 1142 | } |
| 1143 | } |
| 1144 | } |
| 1145 | |
| 1146 | void negative_mutated_by_ref() { |
| 1147 | bool onFire = isBurning(); |
| 1148 | bool &fireRef = onFire; |
| 1149 | if (onFire) { |
| 1150 | tryToExtinguish(fireRef); |
| 1151 | if (onFire) { |
| 1152 | // NO-MESSAGE: fire may have been extinguished |
| 1153 | scream(); |
| 1154 | } |
| 1155 | } |
| 1156 | } |
| 1157 | |
| 1158 | // Volatile |
| 1159 | |
| 1160 | void negatvie_volatile() { |
| 1161 | bool volatile onFire = isBurning(); |
| 1162 | if (onFire) { |
| 1163 | if (onFire) { |
| 1164 | // NO-MESSAGE: maybe some other thread extinguished the fire |
| 1165 | scream(); |
| 1166 | } |
| 1167 | } |
| 1168 | } |
| 1169 | |
| 1170 | void negative_else_branch(bool isHot) { |
| 1171 | bool onFire = isBurning(); |
| 1172 | if (onFire) { |
| 1173 | tryPutFireOut(); |
| 1174 | } else { |
| 1175 | if (isHot && onFire) { |
| 1176 | // NO-MESSAGE: new check is in the `else` branch |
| 1177 | // FIXME: handle `else` branches and negated conditions |
| 1178 | scream(); |
| 1179 | } |
| 1180 | } |
| 1181 | } |
| 1182 | |
| 1183 | // Lambda / block captures. |
| 1184 | |
| 1185 | template <typename T> void accept_callback(T t) { |
| 1186 | // Potentially call the callback. |
| 1187 | // Possibly on a background thread or something. |
| 1188 | } |
| 1189 | |
| 1190 | void accept_block(void (^)(void)) { |
| 1191 | // Potentially call the callback. |
| 1192 | // Possibly on a background thread or something. |
| 1193 | } |
| 1194 | |
| 1195 | void wait(void) { |
| 1196 | // Wait for the previously passed callback to be called. |
| 1197 | } |
| 1198 | |
| 1199 | void capture_and_mutate_by_lambda() { |
| 1200 | bool x = true; |
| 1201 | accept_callback(t: [&]() { x = false; }); |
| 1202 | if (x) { |
| 1203 | wait(); |
| 1204 | if (x) { |
| 1205 | } |
| 1206 | } |
| 1207 | } |
| 1208 | |
| 1209 | void lambda_capture_by_value() { |
| 1210 | bool x = true; |
| 1211 | accept_callback(t: [x]() { if (x) {} }); |
| 1212 | if (x) { |
| 1213 | wait(); |
| 1214 | if (x) { |
| 1215 | // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant condition 'x' [bugprone-redundant-branch-condition] |
| 1216 | } |
| 1217 | } |
| 1218 | } |
| 1219 | |
| 1220 | void capture_by_lambda_but_not_mutate() { |
| 1221 | bool x = true; |
| 1222 | accept_callback(t: [&]() { if (x) {} }); |
| 1223 | if (x) { |
| 1224 | wait(); |
| 1225 | // FIXME: Should warn. |
| 1226 | if (x) { |
| 1227 | } |
| 1228 | } |
| 1229 | } |
| 1230 | |
| 1231 | void capture_and_mutate_by_block() { |
| 1232 | __block bool x = true; |
| 1233 | accept_block(^{ x = false; }); |
| 1234 | if (x) { |
| 1235 | wait(); |
| 1236 | if (x) { |
| 1237 | } |
| 1238 | } |
| 1239 | } |
| 1240 | |
| 1241 | void block_capture_by_value() { |
| 1242 | bool x = true; |
| 1243 | accept_block(^{ if (x) {} }); |
| 1244 | if (x) { |
| 1245 | wait(); |
| 1246 | if (x) { |
| 1247 | // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant condition 'x' [bugprone-redundant-branch-condition] |
| 1248 | } |
| 1249 | } |
| 1250 | } |
| 1251 | |
| 1252 | void capture_by_block_but_not_mutate() { |
| 1253 | __block bool x = true; |
| 1254 | accept_callback(t: ^{ if (x) {} }); |
| 1255 | if (x) { |
| 1256 | wait(); |
| 1257 | // FIXME: Should warn. |
| 1258 | if (x) { |
| 1259 | } |
| 1260 | } |
| 1261 | } |
| 1262 | |
| 1263 | void mutate_at_any_time(bool *x); |
| 1264 | |
| 1265 | void capture_with_branches_inside_lambda_bad() { |
| 1266 | bool x = true; |
| 1267 | accept_callback(t: [=]() { |
| 1268 | if (x) { |
| 1269 | wait(); |
| 1270 | if (x) { |
| 1271 | // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant condition 'x' [bugprone-redundant-branch-condition] |
| 1272 | } |
| 1273 | } |
| 1274 | }); |
| 1275 | mutate_at_any_time(x: &x); |
| 1276 | } |
| 1277 | |
| 1278 | void capture_with_branches_inside_lambda_good() { |
| 1279 | bool x = true; |
| 1280 | accept_callback(t: [&]() { |
| 1281 | if (x) { |
| 1282 | wait(); |
| 1283 | if (x) { |
| 1284 | } |
| 1285 | } |
| 1286 | }); |
| 1287 | mutate_at_any_time(x: &x); |
| 1288 | } |
| 1289 | |
| 1290 | void capture_with_branches_inside_block_bad() { |
| 1291 | bool x = true; |
| 1292 | accept_callback(t: ^{ |
| 1293 | if (x) { |
| 1294 | wait(); |
| 1295 | if (x) { |
| 1296 | // FIXME: Should warn. It currently reacts to &x outside the block |
| 1297 | // which ideally shouldn't have any effect. |
| 1298 | } |
| 1299 | } |
| 1300 | }); |
| 1301 | mutate_at_any_time(x: &x); |
| 1302 | } |
| 1303 | |
| 1304 | void capture_with_branches_inside_block_bad_simpler() { |
| 1305 | bool x = true; |
| 1306 | accept_callback(t: ^{ |
| 1307 | if (x) { |
| 1308 | wait(); |
| 1309 | if (x) { |
| 1310 | // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant condition 'x' [bugprone-redundant-branch-condition] |
| 1311 | } |
| 1312 | } |
| 1313 | }); |
| 1314 | } |
| 1315 | |
| 1316 | void capture_with_branches_inside_block_good() { |
| 1317 | __block bool x = true; |
| 1318 | accept_callback(t: ^{ |
| 1319 | if (x) { |
| 1320 | wait(); |
| 1321 | if (x) { |
| 1322 | } |
| 1323 | } |
| 1324 | }); |
| 1325 | mutate_at_any_time(&x); |
| 1326 | } |
| 1327 | |
| 1328 | // GNU Expression Statements |
| 1329 | |
| 1330 | void negative_gnu_expression_statement() { |
| 1331 | bool onFire = isBurning(); |
| 1332 | if (({ doSomething(); onFire; })) { |
| 1333 | tryToExtinguish(onFire); |
| 1334 | if (({ doSomething(); onFire; })) { |
| 1335 | // NO-MESSAGE: fire may have been extinguished |
| 1336 | scream(); |
| 1337 | } |
| 1338 | } |
| 1339 | } |
| 1340 | |
| 1341 | // Comma after Condition |
| 1342 | |
| 1343 | void negative_comma_after_condition() { |
| 1344 | bool onFire = isBurning(); |
| 1345 | if (doSomething(), onFire) { |
| 1346 | tryToExtinguish(onFire); |
| 1347 | if (doSomething(), onFire) { |
| 1348 | // NO-MESSAGE: fire may have been extinguished |
| 1349 | scream(); |
| 1350 | } |
| 1351 | } |
| 1352 | } |
| 1353 | |
| 1354 | //===--- Unhandled Cases --------------------------------------------------===// |
| 1355 | |
| 1356 | void negated_in_else() { |
| 1357 | bool onFire = isBurning(); |
| 1358 | if (onFire) { |
| 1359 | scream(); |
| 1360 | } else { |
| 1361 | if (!onFire) { |
| 1362 | doSomething(); |
| 1363 | } |
| 1364 | } |
| 1365 | } |
| 1366 | |
| 1367 | void equality() { |
| 1368 | if (peopleInTheBuilding == 1) { |
| 1369 | if (peopleInTheBuilding == 1) { |
| 1370 | doSomething(); |
| 1371 | } |
| 1372 | } |
| 1373 | } |
| 1374 | |
| 1375 | void relational_operator() { |
| 1376 | if (peopleInTheBuilding > 2) { |
| 1377 | if (peopleInTheBuilding > 1) { |
| 1378 | doSomething(); |
| 1379 | } |
| 1380 | } |
| 1381 | } |
| 1382 | |
| 1383 | void relational_operator_reversed() { |
| 1384 | if (peopleInTheBuilding > 1) { |
| 1385 | if (1 < peopleInTheBuilding) { |
| 1386 | doSomething(); |
| 1387 | } |
| 1388 | } |
| 1389 | } |
| 1390 | |
| 1391 | void volatile_concrete_address() { |
| 1392 | // No warning. The value behind the volatile concrete address |
| 1393 | // is beyond our control. It may change at any time. |
| 1394 | if (*(volatile int *)0x1234) { |
| 1395 | if (*(volatile int *)0x1234) { |
| 1396 | doSomething(); |
| 1397 | } |
| 1398 | } |
| 1399 | } |
| 1400 | |