noverify icon indicating copy to clipboard operation
noverify copied to clipboard

Find suspicious duplicated assignments

Open quasilyte opened this issue 3 years ago • 1 comments

PVS Studio is famous for its checks for copy/paste errors.

For example, there is a chance of writing this kind of code:

$x[0] = $v0;
$x[0] = $v1;

Or this:

$x[0] = $v0;
$x[1] = $v0;

It could be intermixed with something else:

// Questionable case: if unrelated() call can affect $x or $v variables,
// we should not report this code;
// but if unrelated() is pure, it should be safe?
$x[0] = $v0;
unrelated();
$x[1] = $v0;

Also bad:

$x[0] = $y;
$x[0] = $y;

We detect some cases with "unused" variable check. But it's worthwhile to verify that we don't miss some of these cases.

There are pitfalls that should be considered here to avoid false positives:

  • RHS should probably be pure
  • RHS (and LHS) should probably not contain the previously assigned value
  • and so on

These blocks should be analysed as "windows" of somewhat fixed size.

quasilyte avatar Apr 19 '21 09:04 quasilyte

It looks interesting.

i582 avatar Apr 19 '21 09:04 i582