Skip to content

Add new flag -Wreturn-mismatch #82872

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

Merged
merged 3 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ Deprecated Compiler Flags

Modified Compiler Flags
-----------------------
- Added a new diagnostic flag ``-Wreturn-mismatch`` which is grouped under
``-Wreturn-type``, and moved some of the diagnostics previously controlled by
``-Wreturn-type`` under this new flag. Fixes #GH72116.

Removed Compiler Flags
-------------------------
Expand Down
4 changes: 3 additions & 1 deletion clang/include/clang/Basic/DiagnosticGroups.td
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,9 @@ def GNURedeclaredEnum : DiagGroup<"gnu-redeclared-enum">;
def RedundantMove : DiagGroup<"redundant-move">;
def Register : DiagGroup<"register", [DeprecatedRegister]>;
def ReturnTypeCLinkage : DiagGroup<"return-type-c-linkage">;
def ReturnType : DiagGroup<"return-type", [ReturnTypeCLinkage]>;
def ReturnMismatch : DiagGroup<"return-mismatch">;
def ReturnType : DiagGroup<"return-type", [ReturnTypeCLinkage, ReturnMismatch]>;

def BindToTemporaryCopy : DiagGroup<"bind-to-temporary-copy",
[CXX98CompatBindToTemporaryCopy]>;
def SelfAssignmentField : DiagGroup<"self-assign-field">;
Expand Down
6 changes: 3 additions & 3 deletions clang/include/clang/Basic/DiagnosticSemaKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -10212,14 +10212,14 @@ def warn_second_parameter_to_va_arg_never_compatible : Warning<

def warn_return_missing_expr : Warning<
"non-void %select{function|method}1 %0 should return a value">, DefaultError,
InGroup<ReturnType>;
InGroup<ReturnMismatch>;
def ext_return_missing_expr : ExtWarn<
"non-void %select{function|method}1 %0 should return a value">, DefaultError,
InGroup<ReturnType>;
InGroup<ReturnMismatch>;
def ext_return_has_expr : ExtWarn<
"%select{void function|void method|constructor|destructor}1 %0 "
"should not return a value">,
DefaultError, InGroup<ReturnType>;
DefaultError, InGroup<ReturnMismatch>;
def ext_return_has_void_expr : Extension<
"void %select{function|method|block}1 %0 should not return void expression">;
def err_return_init_list : Error<
Expand Down
1 change: 1 addition & 0 deletions clang/test/Misc/warning-wall.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ CHECK-NEXT: -Wreorder-ctor
CHECK-NEXT: -Wreorder-init-list
CHECK-NEXT: -Wreturn-type
CHECK-NEXT: -Wreturn-type-c-linkage
CHECK-NEXT: -Wreturn-mismatch
CHECK-NEXT: -Wself-assign
CHECK-NEXT: -Wself-assign-overloaded
CHECK-NEXT: -Wself-assign-field
Expand Down
36 changes: 36 additions & 0 deletions clang/test/Sema/return-type-mismatch.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// RUN: %clang_cc1 -Wreturn-type -Wno-return-mismatch -fsyntax-only -verify=return-type %s
// RUN: %clang_cc1 -Wno-return-type -Wreturn-mismatch -fsyntax-only -verify=return-mismatch %s

int foo(void) __attribute__((noreturn));
int bar(void);

void test1(void) {
return 1; // return-mismatch-warning{{void function 'test1' should not return a value}}
}

int test2(void) {
return; // return-mismatch-warning{{non-void function 'test2' should return a value}}
}

int test3(void) {
// return-type-warning@+1 {{non-void function does not return a value}}
}

int test4(void) {
(void)(bar() || foo()); // return-type-warning@+1 {{non-void function does not return a value in all control paths}}
}

void test5(void) {
} // no-warning

int test6(void) {
return 0; // no-warning
}

int test7(void) {
foo(); // no warning
}

int test8(void) {
bar(); // return-type-warning@+1 {{non-void function does not return a value}}
}