Skip to content

Commit 99ca38c

Browse files
JoePerchestorvalds
authored andcommitted
checkpatch: warn on self-assignments
The uninitialized_var() macro was removed recently via commit 63a0895 ("compiler: Remove uninitialized_var() macro") as it's not a particularly useful warning and its use can "paper over real bugs". Add a checkpatch test to warn on self-assignments as a means to avoid compiler warnings and as a back-door mechanism to reproduce the old uninitialized_var macro behavior. [[email protected]: coding style fixes] Signed-off-by: Joe Perches <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Cc: Kees Cook <[email protected]> Cc: Gustavo A. R. Silva <[email protected]> Cc: Denis Efremov <[email protected]> Cc: Julia Lawall <[email protected]> Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Linus Torvalds <[email protected]>
1 parent c12093a commit 99ca38c

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

scripts/checkpatch.pl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3899,6 +3899,17 @@ sub process {
38993899
#ignore lines not being added
39003900
next if ($line =~ /^[^\+]/);
39013901

3902+
# check for self assignments used to avoid compiler warnings
3903+
# e.g.: int foo = foo, *bar = NULL;
3904+
# struct foo bar = *(&(bar));
3905+
if ($line =~ /^\+\s*(?:$Declare)?([A-Za-z_][A-Za-z\d_]*)\s*=/) {
3906+
my $var = $1;
3907+
if ($line =~ /^\+\s*(?:$Declare)?$var\s*=\s*(?:$var|\*\s*\(?\s*&\s*\(?\s*$var\s*\)?\s*\)?)\s*[;,]/) {
3908+
WARN("SELF_ASSIGNMENT",
3909+
"Do not use self-assignments to avoid compiler warnings\n" . $herecurr);
3910+
}
3911+
}
3912+
39023913
# check for dereferences that span multiple lines
39033914
if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
39043915
$line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {

0 commit comments

Comments
 (0)