Skip to main content
Sonar.tv
Back
Intro to C++ Quick Fixes in VS Code with SonarQube for IDENow Playing

Intro to C++ Quick Fixes in VS Code with SonarQube for IDE

SonarQube for IDEMarch 13th 20234:09

A hands-on tutorial showing how SonarQube for IDE's C++ quick-fix suggestions work inside VS Code, letting developers resolve issues with a single click without leaving the editor.

Introduction to Quick Fixes

SonarLint for VS Code introduces quick fixes, a powerful feature designed to make code maintenance effortless. Quick fixes automatically transform code to align with best practices and modernization standards, eliminating the tedious manual rewrites developers would otherwise need to perform. When SonarLint detects a code issue, users can click the quick fix action to have the problematic code rewritten automatically, or use the keyboard shortcut Command+Dot on Mac or Control+Dot on Windows and Linux to access the feature without leaving the keyboard.

Modernizing Legacy C++ Patterns

One of the most practical applications of SonarLint's quick fixes is modernizing older C++ code patterns. For instance, traditional typedef declarations can be automatically converted to using declarations, a more modern approach that provides better readability, especially when dealing with complex type definitions involving multiple levels of indirection. This transformation is particularly valuable because manually converting complex typedefs is error-prone and time-consuming. Additionally, SonarLint can help developers leverage C++17 and C++20 features, such as nested namespace definitions and inline namespaces, automatically compacting verbose syntax that would otherwise require manual refactoring.

Managing Move Semantics and Type Deduction

SonarLint's quick fixes also address common mistakes with move semantics introduced in C++11. The tool identifies unnecessary std::move calls—such as when std::move is passed to a const reference argument (which cannot bind to an rvalue) or when a type lacks a move constructor—and offers automatic removal. Similarly, the feature assists developers in converting verbose type declarations to auto, simplifying iterator declarations and other verbose type assignments. However, the tool remains cautious about conversions that would change code behavior, such as converting plain iterators to constant iterators, highlighting only safe transformations and leaving potentially problematic ones for developer review.

Exception Handling Best Practices

Exception handling represents another area where quick fixes prove invaluable. SonarLint detects improper catch block ordering, where derived exceptions are unreachable because a base class exception handler appears first. Rather than offering a single solution, the tool provides three quick fix options: reordering the catch blocks to put derived classes before base classes, or removing redundant catch blocks entirely. This flexibility allows developers to choose the solution that best fits their specific exception handling strategy while maintaining code correctness.

Key Takeaways

  • SonarLint quick fixes automate code transformations, reducing the manual effort required to modernize and maintain C++ codebases
  • The feature supports modernization of legacy patterns like typedef to using declarations and helps leverage C++17 and C++20 language features
  • Quick fixes intelligently avoid transformations that would alter code behavior, while still detecting common mistakes like unnecessary std::move calls
  • Exception handling improvements help prevent unreachable code by identifying and fixing improper catch block ordering
  • The plugin is readily available in the VS Code marketplace, making code quality improvements accessible with minimal setup