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++-19: Incorrect warning unused-private-field even though member is used in constructor. #97365

Closed
greg7mdp opened this issue Jul 1, 2024 · 4 comments
Labels
clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer false-positive Warning fires when it should not question A question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead!

Comments

@greg7mdp
Copy link

greg7mdp commented Jul 1, 2024

test program: Wunused-private-field.cpp

// compiling this with `clang++-19 -Wall -c Wunused-private-field.cpp`
//
// produces a warning (even though `c` is used in the lambda)
//   `Wunused-private-field.cpp:19:13: warning: private field 'c' is not used [-Wunused-private-field]`
// ----------------------------------------------------------------------------------------------------
#include <functional>

struct node;

struct cluster {
   void ping();
};

void register_cb(std::function<void()>);

struct node {
   node(cluster&);
private:
   cluster &c;
};

node::node(cluster& c) : c(c) {
   register_cb([&]() { c.ping(); });
   c.ping();
}

compiler output:

~/test ❯ clang++-19 -Wall -c Wunused-private-field.cpp                                                                                                                                     
Wunused-private-field.cpp:19:13: warning: private field 'c' is not used [-Wunused-private-field]
   19 |    cluster &c;
      |             ^
1 warning generated.

compiler version

~/test ❯ clang++-19 --version                                                                                                                                                              
Ubuntu clang version 19.0.0 (++20240630031615+ac84ada9a169-1~exp1~20240630151739.1777)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/lib/llvm-19/bin
@github-actions github-actions bot added the clang Clang issues not falling into any other category label Jul 1, 2024
@EugeneZelenko EugeneZelenko added clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer false-positive Warning fires when it should not and removed clang Clang issues not falling into any other category labels Jul 2, 2024
@5chmidti
Copy link
Contributor

5chmidti commented Jul 2, 2024

I don't think this is a false-positive because you are not using the field in the body, but the parameter. Reduced:

struct S {
    S(int& c) : c(c) { ++c; }

   private:
    int& c;
};
TranslationUnitDecl
`-CXXRecordDecl <line:1:1, line:6:1> line:1:8 struct S definition
  |-DefinitionData pass_in_registers trivially_copyable has_user_declared_ctor can_const_default_init
  | |-DefaultConstructor
  | |-CopyConstructor simple trivial has_const_param needs_implicit implicit_has_const_param
  | |-MoveConstructor exists simple trivial needs_implicit
  | |-CopyAssignment trivial has_const_param needs_implicit implicit_has_const_param
  | |-MoveAssignment exists trivial needs_implicit
  | `-Destructor simple irrelevant trivial needs_implicit
  |-CXXRecordDecl <col:1, col:8> col:8 implicit referenced struct S
  |-CXXConstructorDecl <line:2:5, col:29> col:5 S 'void (int &)' implicit-inline
  | |-ParmVarDecl <col:7, col:12> col:12 used c 'int &'
  | |-CXXCtorInitializer Field 0xde64f38 'c' 'int &'
  | | `-DeclRefExpr <col:19> 'int' lvalue ParmVar 0xde64d28 'c' 'int &'
  | `-CompoundStmt <col:22, col:29>
  |   `-UnaryOperator <col:24, col:26> 'int' lvalue prefix '++'
  |     `-DeclRefExpr <col:26> 'int' lvalue ParmVar 0xde64d28 'c' 'int &'     # references the parameter, not the field
  |-AccessSpecDecl <line:4:4, col:11> col:4 private
  `-FieldDecl <line:5:5, col:10> col:10 c 'int &'

@greg7mdp
Copy link
Author

greg7mdp commented Jul 2, 2024

What? Why should I get a warning that a class member is not used, when it clearly is used. Also the warning is not on the constructor, but where the class member is declared.

@dwblaikie
Copy link
Collaborator

What? Why should I get a warning that a class member is not used, when it clearly is used. Also the warning is not on the constructor, but where the class member is declared.

Because the member isn't used - the parameter is used. the name c in the ctor is referencing the parameter, not the member - so the member is unused.

@greg7mdp
Copy link
Author

greg7mdp commented Jul 3, 2024

@dwblaikie you are right, my bad, thanks.

Not an issue, sorry for the incorrect report.

@greg7mdp greg7mdp closed this as completed Jul 3, 2024
@EugeneZelenko EugeneZelenko added the question A question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead! label Jul 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer false-positive Warning fires when it should not question A question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead!
Projects
None yet
Development

No branches or pull requests

4 participants