‘Quick Fix’ your C++ issues with SonarLint

by geoffray adde and kirti joshi|

C++ quick-fixes, with SonarLint CLion

​When the team decided to implement quick fixes for C++, we committed to bringing value to the C++ user by providing more than what they had today. It appears we found multiple ways to do that. 

First, by providing an enhanced version of the checks natively available through the IDE and other sources. Second, through checks for which quick fixes weren't very common and those which could be made available with SonarLint for other supported IDEs. And finally, unique quick fixes exclusive to SonarLint! 

Let’s take a peek at what we did (and why) and how this can help you efficiently deliver cleaner, safer C++ code.  

Common Fixes. Made Better.

For instance, const-correctness is the foundation of many checks that the semantics of C++ defines to avoid many bugs. We have multiple rules checking const-correctness in your C++ code. But now we also took special care in being able to track most mutations. This way we are able to detect and fix more issues than other implementations.
The rule S5350 would fix:

to

And the rule S5817 would fix

to

Uncommon fixes across supported IDEs

Another way for us to bring value was to address fixes that are not available on each IDE supported by SonarLint.

Rule S5416 helps improve the readability and consistency of your code. It would fix a C typedef

to a C++ using

Rule S1905 implements another non-exclusive and very useful fix that would remove redundant casts, thus making your code more readable and less confusing. It would fix:

to

Exclusive Quick Fixes

Finally, a very rewarding way for us to bring additional value was with exclusive quick fixes.

For example, rule S5952 would avoid useless and error-prone redundancy while manually making constructors from a base class available in a derived class. It would fix:

to

Rule S6030 focuses on performance and would make sure that pairs are created for insertion in maps only when strictly necessary. It would fix:

to

All the fixes demonstrated above and over 40 other quick fixes are already available in SonarLint for CLion. Once you install SonarLint and run through your code, you can use the IDE native functionality (mouse over/keystrokes) to quickly view the flagged issue and the associated rule violation. Then simply apply the suggested quick fix.

These C++ quick fixes will soon be available in Visual Studio and Eclipse with SonarLint as well!  And we also plan to have these supported in SonarLint for VSCode as we roll out the C++ support for VS Code fairly soon. So stay tuned by watching this blog space or through SonarLint product news notifications. 

Are you ready to give these a try?