1*4882a593Smuzhiyun// SPDX-License-Identifier: GPL-2.0-only 2*4882a593Smuzhiyun///Find conditions where if and else branch are functionally 3*4882a593Smuzhiyun// identical. 4*4882a593Smuzhiyun// 5*4882a593Smuzhiyun// There can be false positives in cases where the positional 6*4882a593Smuzhiyun// information is used (as with lockdep) or where the identity 7*4882a593Smuzhiyun// is a placeholder for not yet handled cases. 8*4882a593Smuzhiyun// Unfortunately there also seems to be a tendency to use 9*4882a593Smuzhiyun// the last if else/else as a "default behavior" - which some 10*4882a593Smuzhiyun// might consider a legitimate coding pattern. From discussion 11*4882a593Smuzhiyun// on kernelnewbies though it seems that this is not really an 12*4882a593Smuzhiyun// accepted pattern and if at all it would need to be commented 13*4882a593Smuzhiyun// 14*4882a593Smuzhiyun// In the Linux kernel it does not seem to actually report 15*4882a593Smuzhiyun// false positives except for those that were documented as 16*4882a593Smuzhiyun// being intentional. 17*4882a593Smuzhiyun// the two known cases are: 18*4882a593Smuzhiyun// arch/sh/kernel/traps_64.c:read_opcode() 19*4882a593Smuzhiyun// } else if ((pc & 1) == 0) { 20*4882a593Smuzhiyun// /* SHcompact */ 21*4882a593Smuzhiyun// /* TODO : provide handling for this. We don't really support 22*4882a593Smuzhiyun// user-mode SHcompact yet, and for a kernel fault, this would 23*4882a593Smuzhiyun// have to come from a module built for SHcompact. */ 24*4882a593Smuzhiyun// return -EFAULT; 25*4882a593Smuzhiyun// } else { 26*4882a593Smuzhiyun// /* misaligned */ 27*4882a593Smuzhiyun// return -EFAULT; 28*4882a593Smuzhiyun// } 29*4882a593Smuzhiyun// fs/kernfs/file.c:kernfs_fop_open() 30*4882a593Smuzhiyun// * Both paths of the branch look the same. They're supposed to 31*4882a593Smuzhiyun// * look that way and give @of->mutex different static lockdep keys. 32*4882a593Smuzhiyun// */ 33*4882a593Smuzhiyun// if (has_mmap) 34*4882a593Smuzhiyun// mutex_init(&of->mutex); 35*4882a593Smuzhiyun// else 36*4882a593Smuzhiyun// mutex_init(&of->mutex); 37*4882a593Smuzhiyun// 38*4882a593Smuzhiyun// All other cases look like bugs or at least lack of documentation 39*4882a593Smuzhiyun// 40*4882a593Smuzhiyun// Confidence: Moderate 41*4882a593Smuzhiyun// Copyright: (C) 2016 Nicholas Mc Guire, OSADL. 42*4882a593Smuzhiyun// Comments: 43*4882a593Smuzhiyun// Options: --no-includes --include-headers 44*4882a593Smuzhiyun 45*4882a593Smuzhiyunvirtual org 46*4882a593Smuzhiyunvirtual report 47*4882a593Smuzhiyun 48*4882a593Smuzhiyun@cond@ 49*4882a593Smuzhiyunstatement S1; 50*4882a593Smuzhiyunposition p; 51*4882a593Smuzhiyun@@ 52*4882a593Smuzhiyun 53*4882a593Smuzhiyun* if@p (...) S1 else S1 54*4882a593Smuzhiyun 55*4882a593Smuzhiyun@script:python depends on org@ 56*4882a593Smuzhiyunp << cond.p; 57*4882a593Smuzhiyun@@ 58*4882a593Smuzhiyun 59*4882a593Smuzhiyuncocci.print_main("WARNING: possible condition with no effect (if == else)",p) 60*4882a593Smuzhiyun 61*4882a593Smuzhiyun@script:python depends on report@ 62*4882a593Smuzhiyunp << cond.p; 63*4882a593Smuzhiyun@@ 64*4882a593Smuzhiyun 65*4882a593Smuzhiyuncoccilib.report.print_report(p[0],"WARNING: possible condition with no effect (if == else)") 66