Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[clang-tidy] when try to use code action to fix the function pointer not initialized problem, clang-tidy add =nullptr to the wrong place #112089

Open
LXYan2333 opened this issue Oct 12, 2024 · 1 comment · May be fixed by #112091
Assignees
Labels
bug Indicates an unexpected problem or unintended behavior clang-tidy

Comments

@LXYan2333
Copy link

code example:

int main() {
    void(*f)(void*);
}

the function pointer f is not initialized, and clang-tidy suggest to add =nullptr to initialize it. but clang-tidy add it to a wrong position:

code example from @HighCommander4

$ cat test.cpp
int main() {
    void(*f)(void*);
}

$ clang-tidy --checks=cppcoreguidelines-init-variables --fix test.cpp
Running without flags.
1 warning generated.
test.cpp:2:11: warning: variable 'f' is not initialized [cppcoreguidelines-init-variables]
    2 |     void(*f)(void*);
      |           ^
      |             = nullptr
test.cpp:2:12: note: FIX-IT applied suggested code changes
    2 |     void(*f)(void*);
      |            ^
clang-tidy applied 1 of 1 suggested fixes.

$ cat test.cpp
int main() {
    void(*f = nullptr)(void*);
}

expected behaviour:

int main() {
    void(*f)(void*)=nullptr;
}
@llvmbot
Copy link
Collaborator

llvmbot commented Oct 12, 2024

@llvm/issue-subscribers-bug

Author: None (LXYan2333)

code example:
int main() {
    void(*f)(void*);
}

the function pointer f is not initialized, and clang-tidy suggest to add =nullptr to initialize it. but clang-tidy add it to a wrong position:

code example from @HighCommander4

$ cat test.cpp
int main() {
    void(*f)(void*);
}

$ clang-tidy --checks=cppcoreguidelines-init-variables --fix test.cpp
Running without flags.
1 warning generated.
test.cpp:2:11: warning: variable 'f' is not initialized [cppcoreguidelines-init-variables]
    2 |     void(*f)(void*);
      |           ^
      |             = nullptr
test.cpp:2:12: note: FIX-IT applied suggested code changes
    2 |     void(*f)(void*);
      |            ^
clang-tidy applied 1 of 1 suggested fixes.

$ cat test.cpp
int main() {
    void(*f = nullptr)(void*);
}

expected behaviour:

int main() {
    void(*f)(void*)=nullptr;
}

5chmidti added a commit to 5chmidti/llvm-project that referenced this issue Oct 12, 2024
…uidelines-init-variables

Previously, the insertion location for the `= nullptr` fix would be
after the variable name. However, if the variable is of type function
pointer that is not an alias, then the insertion would happen inside the
type specification: `void (*a1)(void*);` -> `void (*a1 = nullptr)(void*);`.
With this change, the insertion location will be at the next
'terminator'. That is, at the next `,` or `;`, as that will finish the
current declaration: `void (a1)(void*) = nullptr;`.

Fixes llvm#112089
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Indicates an unexpected problem or unintended behavior clang-tidy
Projects
None yet
3 participants