[clang] [clang-tools-extra] [Clang] Implement CWG2813: Class member access with prvalues (PR #95112)
Mital Ashok via cfe-commits
cfe-commits at lists.llvm.org
Fri Jun 14 01:12:41 PDT 2024
================
@@ -387,9 +388,16 @@ void Sema::DiagnoseUnusedExprResult(const Stmt *S, unsigned DiagID) {
// Do not diagnose use of a comma operator in a SFINAE context because the
// type of the left operand could be used for SFINAE, so technically it is
// *used*.
- if (DiagID != diag::warn_unused_comma_left_operand || !isSFINAEContext())
- DiagIfReachable(Loc, S ? llvm::ArrayRef(S) : std::nullopt,
- PDiag(DiagID) << R1 << R2);
+ if (DiagID == diag::warn_unused_comma_left_operand && isSFINAEContext())
+ return;
+
+ // Don't diagnose discarded left of dot in static class member access
+ // because its type is "used" to determine the class to access
+ if (OrigDiagID == diag::warn_discarded_class_member_access)
+ return;
----------------
MitalAshok wrote:
This code path is called for all static member accesses, not just on prvalues.
The problem is in stuff like `discarded.static_member_fn();`. This function is called for `(discarded, E2)` and `discarded;` (expression-statement), where those were previously the only two discarded-value expressions. These had an easy fix of casting to `void`, but that doesn't work for this new discarded-value. https://godbolt.org/z/9b949rvxc
I think that this warning shouldn't be applied when the thing that is discarded is/was dependently typed. Maybe for `s.x()` if we have a declaration `S s;` we can suggest `S::x()`, but that shouldn't be on by default. Or `S().x()` into `S::x()` if that constructor call has no side-effects. Maybe that is more appropriate for clang-tidy than the compiler though?
For now this PR doesn't introduce any new diagnostics, just existing warnings about [[nodiscard]] values being discarded in new positions. I think I'll just split some of `Sema::DiagnoseUnusedExprResult` into `Sema::DiagnoseDiscardedValue` for specifically checking [[nodiscard]] stuff and call that instead without a new diagnostic
https://github.com/llvm/llvm-project/pull/95112
More information about the cfe-commits
mailing list