|
| 1 | +# functionConst |
| 2 | + |
| 3 | +**Message**: Technically the member function 'x' can be const<br/> |
| 4 | +**Category**: Robustness<br/> |
| 5 | +**Severity**: Style (Inconclusive)<br/> |
| 6 | +**Language**: C++ |
| 7 | + |
| 8 | +## Description |
| 9 | + |
| 10 | +This checker identifies member functions that do not modify any member variables and therefore could be declared as `const`. A const member function promises not to modify the object's state and enables the function to be called on const objects. |
| 11 | + |
| 12 | +The danger is that a const member function is allowed to have side effects outside the object. If you create a member function that formats the hard drive using |
| 13 | +system calls it might be technically possible to make the function const, but is it "correct"? Using a const object is not supposed to have side effects. |
| 14 | + |
| 15 | +For methods that has no side effects whatsoever; making them const is recommended. |
| 16 | + |
| 17 | +The checker analyzes member functions and detects when: |
| 18 | +- The function only reads member variables (never writes to them) |
| 19 | +- The function only calls other const member functions |
| 20 | +- The function only modifies parameters passed by reference or pointer (not member variables) |
| 21 | +- The function performs operations that don't change the object's logical state |
| 22 | + |
| 23 | +This warning is marked as **inconclusive** because while the function can technically be made const, it may not always be "correct". |
| 24 | + |
| 25 | +This warning helps improve code quality by: |
| 26 | +- Making the function's non-modifying nature explicit |
| 27 | +- Enabling the function to be called on const objects |
| 28 | +- Improving const-correctness throughout the codebase |
| 29 | +- Helping with compiler optimizations |
| 30 | +- Making code intentions clearer to other developers |
| 31 | + |
| 32 | +## Motivation |
| 33 | + |
| 34 | +The motivation of this checker is to improve robustness by making the code more const-correct. |
| 35 | + |
| 36 | +## How to fix |
| 37 | + |
| 38 | +Add the `const` keyword after the function signature to indicate that the function does not modify the object's state. |
| 39 | + |
| 40 | +Before: |
| 41 | +```cpp |
| 42 | +class Rectangle { |
| 43 | + int width, height; |
| 44 | +public: |
| 45 | + int getWidth() { return width; } |
| 46 | + int getHeight() { return height; } |
| 47 | + int getArea() { return width * height; } |
| 48 | + |
| 49 | + void printInfo() { |
| 50 | + std::cout << "Width: " << width << ", Height: " << height << std::endl; |
| 51 | + } |
| 52 | + |
| 53 | + bool isSquare() { |
| 54 | + return width == height; |
| 55 | + } |
| 56 | + |
| 57 | + void copyDataTo(Rectangle& other) { |
| 58 | + other.width = width; |
| 59 | + other.height = height; |
| 60 | + } |
| 61 | +}; |
| 62 | +``` |
| 63 | + |
| 64 | +After: |
| 65 | +```cpp |
| 66 | +class Rectangle { |
| 67 | + int width, height; |
| 68 | +public: |
| 69 | + int getWidth() const { return width; } |
| 70 | + int getHeight() const { return height; } |
| 71 | + int getArea() const { return width * height; } |
| 72 | + |
| 73 | + void printInfo() const { |
| 74 | + std::cout << "Width: " << width << ", Height: " << height << std::endl; |
| 75 | + } |
| 76 | + |
| 77 | + bool isSquare() const { |
| 78 | + return width == height; |
| 79 | + } |
| 80 | + |
| 81 | + void copyDataTo(Rectangle& other) const { |
| 82 | + other.width = width; |
| 83 | + other.height = height; |
| 84 | + } |
| 85 | +}; |
| 86 | +``` |
| 87 | + |
| 88 | +## Related checkers |
| 89 | + |
| 90 | +- `functionStatic` - for member functions that can be declared static |
| 91 | +- `constParameter` - for function parameters that can be const |
| 92 | +- `constParameterReference` - for reference parameters that can be const |
| 93 | +- `constParameterPointer` - for pointer parameters that can be const |
| 94 | +- `constVariable` - for local variables that can be const |
| 95 | +- `constVariableReference` - for local reference variables that can be const |
| 96 | + |
| 97 | +## Notes |
| 98 | + |
| 99 | +- This check is marked as **inconclusive** because the decision to make a function const should also consider the conceptual design |
| 100 | +- Virtual functions should be carefully considered before making them const, as this affects the entire inheritance hierarchy |
| 101 | +- Think about whether the function's purpose is to query state (should be const) or to perform an action (may not need to be const) |
0 commit comments