162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 262306a36Sopenharmony_ci///Find conditions where if and else branch are functionally 362306a36Sopenharmony_ci// identical. 462306a36Sopenharmony_ci// 562306a36Sopenharmony_ci// There can be false positives in cases where the positional 662306a36Sopenharmony_ci// information is used (as with lockdep) or where the identity 762306a36Sopenharmony_ci// is a placeholder for not yet handled cases. 862306a36Sopenharmony_ci// Unfortunately there also seems to be a tendency to use 962306a36Sopenharmony_ci// the last if else/else as a "default behavior" - which some 1062306a36Sopenharmony_ci// might consider a legitimate coding pattern. From discussion 1162306a36Sopenharmony_ci// on kernelnewbies though it seems that this is not really an 1262306a36Sopenharmony_ci// accepted pattern and if at all it would need to be commented 1362306a36Sopenharmony_ci// 1462306a36Sopenharmony_ci// In the Linux kernel it does not seem to actually report 1562306a36Sopenharmony_ci// false positives except for those that were documented as 1662306a36Sopenharmony_ci// being intentional. 1762306a36Sopenharmony_ci// the two known cases are: 1862306a36Sopenharmony_ci// arch/sh/kernel/traps_64.c:read_opcode() 1962306a36Sopenharmony_ci// } else if ((pc & 1) == 0) { 2062306a36Sopenharmony_ci// /* SHcompact */ 2162306a36Sopenharmony_ci// /* TODO : provide handling for this. We don't really support 2262306a36Sopenharmony_ci// user-mode SHcompact yet, and for a kernel fault, this would 2362306a36Sopenharmony_ci// have to come from a module built for SHcompact. */ 2462306a36Sopenharmony_ci// return -EFAULT; 2562306a36Sopenharmony_ci// } else { 2662306a36Sopenharmony_ci// /* misaligned */ 2762306a36Sopenharmony_ci// return -EFAULT; 2862306a36Sopenharmony_ci// } 2962306a36Sopenharmony_ci// fs/kernfs/file.c:kernfs_fop_open() 3062306a36Sopenharmony_ci// * Both paths of the branch look the same. They're supposed to 3162306a36Sopenharmony_ci// * look that way and give @of->mutex different static lockdep keys. 3262306a36Sopenharmony_ci// */ 3362306a36Sopenharmony_ci// if (has_mmap) 3462306a36Sopenharmony_ci// mutex_init(&of->mutex); 3562306a36Sopenharmony_ci// else 3662306a36Sopenharmony_ci// mutex_init(&of->mutex); 3762306a36Sopenharmony_ci// 3862306a36Sopenharmony_ci// All other cases look like bugs or at least lack of documentation 3962306a36Sopenharmony_ci// 4062306a36Sopenharmony_ci// Confidence: Moderate 4162306a36Sopenharmony_ci// Copyright: (C) 2016 Nicholas Mc Guire, OSADL. 4262306a36Sopenharmony_ci// Comments: 4362306a36Sopenharmony_ci// Options: --no-includes --include-headers 4462306a36Sopenharmony_ci 4562306a36Sopenharmony_civirtual org 4662306a36Sopenharmony_civirtual report 4762306a36Sopenharmony_ci 4862306a36Sopenharmony_ci@cond@ 4962306a36Sopenharmony_cistatement S1; 5062306a36Sopenharmony_ciposition p; 5162306a36Sopenharmony_ci@@ 5262306a36Sopenharmony_ci 5362306a36Sopenharmony_ci* if@p (...) S1 else S1 5462306a36Sopenharmony_ci 5562306a36Sopenharmony_ci@script:python depends on org@ 5662306a36Sopenharmony_cip << cond.p; 5762306a36Sopenharmony_ci@@ 5862306a36Sopenharmony_ci 5962306a36Sopenharmony_cicocci.print_main("WARNING: possible condition with no effect (if == else)",p) 6062306a36Sopenharmony_ci 6162306a36Sopenharmony_ci@script:python depends on report@ 6262306a36Sopenharmony_cip << cond.p; 6362306a36Sopenharmony_ci@@ 6462306a36Sopenharmony_ci 6562306a36Sopenharmony_cicoccilib.report.print_report(p[0],"WARNING: possible condition with no effect (if == else)") 66