GeneralPurpose-StringLiteralToNonConstPointer¶
String literals shall not be assigned to non-constant pointers
Required inputs: IR
const char[] and should never be modified.
Assigning them to non-const pointers is a deprecated conversion that loses type safety and
suggests intent to modify immutable data. The correct approach is to use const char*
or std::string_view for string literals.
Bad code (deprecated conversion):
char* ptr = "hello"; // ERROR: assigning const char[] to char* ptr[0] = 'H'; // Undefined behavior: modifying string literal
Good code (using const pointer):
const char* ptr = "hello"; // OK: const char* for string literal // ptr[0] = 'H'; // Compiler error: cannot modify const data std::cout << ptr;
Good code (using std::string_view):
std::string_view view = "hello"; // OK: string_view for string literal std::cout << view; // Safe to read
Good code (using std::string):
std::string str = "hello"; // OK: creates modifiable copy str[0] = 'H'; // Safe to modify std::cout << str;
Good code (using char array):
char arr[] = "hello"; // OK: a char array is created on the stack arr[0] = 'H';
Possible Messages
Key |
Text |
Severity |
Disabled |
|---|---|---|---|
cafe_message |
{} |
None |
False |
Options¶
This rule shares the following common options: exclude_in_macros, exclude_messages_in_system_headers, excludes, extend_exclude_to_macro_invocations, includes, justification_checker, languages, post_processing, provider, report_at, severity
The following places define options that affect this rule: Stylechecks, Analysis-GlobalOptions
message_predicate¶
message_predicate : typing.Callable[[Cafe_Message], bool] | None = None
True for messages to
report.
reported_messages¶
reported_messages : set[int] | None = {2464}
reported_severities¶
reported_severities : set[str] = {'error', 'remark', 'warning'}
use_error_number¶
use_error_number : bool = False
use_rule_severity¶
use_rule_severity : bool = True