Problem when quickfix for declaring-loop-variables-inside-loop modifies a multi-declaration line

Please provide

  • Operating system:
    Window 10
  • Visual Studio version:
    2022 64-bit Version 17.9.2
  • SonarLint plugin version:
  • Programming language you’re coding in:
    c++ latest standard
  • Is connected mode used:

And a thorough description of the problem / question:

Here’s the code I was going through:

  int i, um = 0;
  int cur_pos = result_size;
  int was_frac = 0;
  for (i = result_scale - 1; i >= 0; i--) {
     /* do stuff with i and um */
/* do more stuff with um */

The first line has a cpp:S1659 (multiple declarations in a single line)
The for loop has a cpp:S5955 (loop variables should be declared in their loop)

If choose the QuickAction to move the declaration into the loop first, the variable um is deleted. Maybe give a warning if an action deletes an unrelated variable? I’m not sure what the best way to handle that is.

This is particularly problematic because these variable names are weird and I’d have a hard time trying to figure out what type um was supposed to be.

Welcome to the community, @AbitFishy!

Thank you very much for the report, we aim for our quick fix to never produce invalid code, and your input is invaluable in helping us reach that goal. I have created ticket CPP-5057, to track the reported issues.

In the meantime, to solve issues in your particular example, you can apply quick-fix for cpp:S1659 (multiple declarations in a single line) first, and then quickfix for cpp:S5955 (loop variables should be declared in their loop), will behave correctly.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.