Issue 157320
Summary [clang-tidy] `misc-const-correctness` false positive on non-const return value
Labels clang-tidy, false-positive
Assignees
Reporter apple1417
    Given the following example:

```cpp
char buf[64];
char* func1(void) {
 char* x = buf;
    return x;
}
char* func2(void) {
    return buf;
}

void test(void) {
    *func1() += 1;
    *func2() += 1;
}
```
[Compiler Explorer](https://godbolt.org/#g:!((g:!((g:!((h:codeEditor,i:(filename:'1',fontScale:14,fontUsePx:'0',j:1,lang:c%2B%2B,selection:(endColumn:2,endLineNumber:13,positionColumn:1,positionLineNumber:1,selectionStartColumn:2,selectionStartLineNumber:13,startColumn:1,startLineNumber:1),source:'char+buf%5B64%5D%3B%0Achar*+func1(void)+%7B%0A++++char*+x+%3D+buf%3B%0A++++return+x%3B%0A%7D%0Achar*+func2(void)+%7B%0A++++return+buf%3B%0A%7D%0A%0Avoid+test(void)+%7B%0A++++*func1()+%2B%3D+1%3B%0A++++*func2()+%2B%3D+1%3B%0A%7D'),l:'5',n:'0',o:'C%2B%2B+source+%231',t:'0')),k:36.88754966907773,l:'4',n:'0',o:'',s:0,t:'0'),(g:!((h:compiler,i:(compiler:clang2110,filters:(b:'0',binary:'1',binaryObject:'1',commentOnly:'0',debugCalls:'1',demangle:'0',directives:'0',execute:'1',intel:'0',libraryCode:'0',trim:'1',verboseDemangling:'0'),flagsViewOpen:'1',fontScale:14,fontUsePx:'0',j:1,lang:c%2B%2B,libs:!(),options:'',overrides:!(),selection:(endColumn:1,endLineNumber:1,positionColumn:1,positionLineNumber:1,selectionStartColumn:1,selectionStartLineNumber:1,startColumn:1,startLineNumber:1),source:1),l:'5',n:'0',o:'+x86-64+clang+21.1.0+(Editor+%231)',t:'0')),k:33.18197865564137,l:'4',n:'0',o:'',s:0,t:'0'),(g:!((h:tool,i:(args:'--checks%3Dmisc-const-correctness',argsPanelShown:'0',compilerName:'x86-64+clang+21.1.0',editorid:1,fontScale:14,fontUsePx:'0',j:1,monacoEditorHasBeenAutoOpened:'1',monacoEditorOpen:'1',monacoStdin:'1',stdin:'',stdinPanelShown:'1',toolId:clangtidytrunk,treeid:0,wrap:'1'),l:'5',n:'0',o:'clang-tidy+(trunk)+x86-64+clang+21.1.0+(Editor+%231,+Compiler+%231)',t:'0')),k:29.930471675280916,l:'4',n:'0',o:'',s:0,t:'0')),l:'2',n:'0',o:'',t:'0')),version:4)

Clang tidy 21.1.0 incorrectly gives a `misc-const-correctness` warning. Clang tidy 20.1.8 reports no issues.
```
PS P:\> clang-tidy test.cpp --checks=misc-const-correctness
Error while trying to load a compilation database:
Could not auto-detect compilation database for file "test.cpp"
No compilation database found in P:\ or any parent directory
fixed-compilation-database: Error while opening fixed database: no such file or directory
json-compilation-database: Error while opening JSON database: no such file or directory
Running without flags.
1 warning generated.
P:\test.cpp:3:5: warning: pointee of variable 'x' of type 'char *' can be declared 'const' [misc-const-correctness]
    3 |     char* x = buf;
      |     ^   
      |          const
```
Trying to apply this change will of course make the code stop compiling.
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to