[clang] [Clang] handle [[warn_unused]] attribute for unused private fields (PR #120734)

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Fri Jan 17 09:13:35 PST 2025


================
@@ -3307,6 +3307,29 @@ void Sema::CheckShadowInheritedFields(const SourceLocation &Loc,
   }
 }
 
+template <typename AttrType>
+inline static bool HasAttribute(const QualType &T) {
----------------
AaronBallman wrote:

Here's my take on it: https://godbolt.org/z/qxq6jf3jM
```
class [[gnu::warn_unused]] S
{
public:
    S();
};

struct [[maybe_unused]] T {};

void f()
{
    int i = 0; // Clang, GCC warn
    S s;       // Clang, GCC warn
    T t;       // Neither warn
}

class C
{
private:
    const int i = 0; // Clang warns
    int j = 0;       // Clang warns
    const S s1;      // Neither warn, Clang should warn
    const T t1;      // Neither warn
    S s2;            // Neither warn, Clang should warn
    T t2;            // Neither warn
};
```
GCC doesn't support `-Wunused-private-field`, so it makes sense that they never warn on unused private fields. GCC doesn't document `warn_unused` as an attribute that I can find (they document `warn_unused_result`) but they do seem to support it. As best I can tell, the distinction between it and `maybe_unused` is that putting the attribute on a type does not silence the diagnostic on uses of that type.

https://github.com/llvm/llvm-project/pull/120734


More information about the cfe-commits mailing list