This is an archive of the discontinued LLVM Phabricator instance.

[clang] Add -Wunused-result-always warning
Needs ReviewPublic

Authored by gpanders on Apr 27 2023, 2:11 PM.

Details

Reviewers
theraven
Summary

This flag enables warnings for unused function return values
universally, even for functions which do not have a "warn_unused_result"
attribute.

This is disabled by default as it is far too noisy for most C/C++ code;
however, there are contexts where this is a desirable warning to have
(for example, in safety critical code where ignoring return values is
usually forbidden).

Diff Detail

Event Timeline

gpanders created this revision.Apr 27 2023, 2:11 PM
Herald added a project: Restricted Project. · View Herald TranscriptApr 27 2023, 2:11 PM
gpanders requested review of this revision.Apr 27 2023, 2:11 PM
Herald added a project: Restricted Project. · View Herald TranscriptApr 27 2023, 2:11 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
gpanders updated this revision to Diff 518024.Apr 28 2023, 12:45 PM

Remove cast to (void) in unused-expr.c

This cast is not necessary for this test, because foo does not have a
"warn_unused_result" attribute and is not marked const or pure. Removing the
cast improves the usefuless of the test as it verifies that there is no warning
issued in such a case (but a warning *would* be issued with
-Wunused-result-always, so this also acts as a "negative case" to ensure no
warning is issued when that flag is not present).

Seems more stylistic/unlikely to meet the diagnostic bar (certtainly couldn't be on-by-default for instance) for clang to me (but could go into something like clang-tidy), at least.

Seems more stylistic/unlikely to meet the diagnostic bar (certtainly couldn't be on-by-default for instance) for clang to me (but could go into something like clang-tidy), at least.

+1 -- we expect new diagnostics to be ones we can enable by default (experience has shown us that off-by-default warnings don't typically get enabled often enough to be worth adding except for special circumstances), and there's basically no way to enable this by default. However, it does seem appropriate as a clang-tidy check: https://clang.llvm.org/extra/clang-tidy/Contributing.html