Skip to content

Commit

Permalink
checkpatch: warn on self-assignments
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
JoePerches authored and torvalds committed Oct 16, 2020
1 parent c12093a commit 99ca38c
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions scripts/checkpatch.pl
Original file line number Diff line number Diff line change
Expand Up @@ -3899,6 +3899,17 @@ sub process {
#ignore lines not being added
next if ($line =~ /^[^\+]/);

# check for self assignments used to avoid compiler warnings
# e.g.: int foo = foo, *bar = NULL;
# struct foo bar = *(&(bar));
if ($line =~ /^\+\s*(?:$Declare)?([A-Za-z_][A-Za-z\d_]*)\s*=/) {
my $var = $1;
if ($line =~ /^\+\s*(?:$Declare)?$var\s*=\s*(?:$var|\*\s*\(?\s*&\s*\(?\s*$var\s*\)?\s*\)?)\s*[;,]/) {
WARN("SELF_ASSIGNMENT",
"Do not use self-assignments to avoid compiler warnings\n" . $herecurr);
}
}

# check for dereferences that span multiple lines
if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
$line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
Expand Down

0 comments on commit 99ca38c

Please sign in to comment.