|
| 1 | +# constParameterPointer |
| 2 | + |
| 3 | +**Message**: Parameter 'x' can be declared as pointer to const<br/> |
| 4 | +**Category**: Robustness<br/> |
| 5 | +**Severity**: Style<br/> |
| 6 | +**Language**: C/C++ |
| 7 | + |
| 8 | +## Description |
| 9 | + |
| 10 | +This checker identifies function parameters that are pointers which are never used to modify the data they point to. Such parameters can be declared as "pointer to const" to improve code clarity and prevent accidental modifications. |
| 11 | + |
| 12 | +The checker analyzes pointer parameters and detects when: |
| 13 | +- The pointer is dereferenced only for reading values (not writing) |
| 14 | +- The pointer is used in comparisons or logical operations |
| 15 | +- The pointer is passed to functions that expect const pointers |
| 16 | +- The pointer arithmetic is performed without modifying the pointed-to data |
| 17 | + |
| 18 | +This warning helps improve code quality by: |
| 19 | +- Making the intent clear that the function will not modify the pointed-to data |
| 20 | +- Enabling compiler optimizations |
| 21 | +- Preventing accidental modifications |
| 22 | +- Improving const-correctness |
| 23 | + |
| 24 | +## Motivation |
| 25 | + |
| 26 | +This checker improves const-correctness. Const-correct code is more robust and easier to understand. |
| 27 | + |
| 28 | +## How to fix |
| 29 | + |
| 30 | +Add the `const` keyword to the parameter declaration to indicate that the pointed-to data will not be modified. |
| 31 | + |
| 32 | +Before: |
| 33 | +```cpp |
| 34 | +void printValue(int* p) { |
| 35 | + printf("%d\n", *p); // Only reading the value |
| 36 | +} |
| 37 | + |
| 38 | +int findMax(int* arr, size_t size) { |
| 39 | + int max = arr[0]; |
| 40 | + for (size_t i = 1; i < size; i++) { |
| 41 | + if (arr[i] > max) { // Only reading array elements |
| 42 | + max = arr[i]; |
| 43 | + } |
| 44 | + } |
| 45 | + return max; |
| 46 | +} |
| 47 | + |
| 48 | +bool isEqual(char* str1, char* str2) { |
| 49 | + return strcmp(str1, str2) == 0; // Only reading strings |
| 50 | +} |
| 51 | +``` |
| 52 | +
|
| 53 | +After: |
| 54 | +```cpp |
| 55 | +void printValue(const int* p) { |
| 56 | + printf("%d\n", *p); // Clearly indicates read-only access |
| 57 | +} |
| 58 | +
|
| 59 | +int findMax(const int* arr, size_t size) { |
| 60 | + int max = arr[0]; |
| 61 | + for (size_t i = 1; i < size; i++) { |
| 62 | + if (arr[i] > max) { |
| 63 | + max = arr[i]; |
| 64 | + } |
| 65 | + } |
| 66 | + return max; |
| 67 | +} |
| 68 | +
|
| 69 | +bool isEqual(const char* str1, const char* str2) { |
| 70 | + return strcmp(str1, str2) == 0; // Standard library functions expect const char* |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +## Related checkers |
| 75 | + |
| 76 | +- `constParameter` - for non-pointer parameters that can be const |
| 77 | +- `constParameterReference` - for reference parameters that can be const |
| 78 | +- `constParameterCallback` - for callback function parameters that can be const |
| 79 | +- `constVariablePointer` - for local pointer variables that can be const |
| 80 | + |
| 81 | +## Notes |
| 82 | + |
| 83 | +- This check is disabled for virtual functions and callback functions where changing the signature might break polymorphism or function pointer compatibility |
| 84 | +- The checker may suggest callback-specific warnings when a function is used as a callback, indicating that const-ification might require casting function pointers |
| 85 | +- Template functions are handled carefully to avoid false positives in generic code |
0 commit comments