Clang warning: taking the absolute value of unsigned type A has no effect [-Wabsolute-value] (warn_unsigned_abs)
Jump to navigation
Jump to search
Text | error: taking the absolute value of unsigned type A has no effect |
---|---|
Type | Warning |
Category | Semantic Issue |
Internal Id | warn_unsigned_abs |
Active by Default | Yes |
Flags | -Wno-absolute-value (4 elements) |
Internal Message | taking the absolute value of unsigned type %0 has no effect
|
Regular Expression | (?:warning|error|fatal error)\: taking the absolute value of unsigned type (.*?) has no effect \[(?:\-Werror,)?\-Wabsolute\-value[^\]]*\]
|
First Commit | 2014-02-26 7eb0b2c1819c Add -Wabsolute-value, warnings about absolute value functions. |
Description
Example
Flags | -xc
|
|
---|---|---|
Source |
#include <stdlib.h>
int main() {
unsigned int a = 10; // unsigned variable
a = abs(a); // abs() on unsigned type
return 0;
}
| |
Compiler Output |
<source>:5:9: warning: taking the absolute value of unsigned type 'unsigned int' has no effect [-Wabsolute-value] <source>:5:9: note: remove the call to 'abs' since unsigned values cannot be negative |
Clang Internals (17.0.6)
Git Commit Message
Add -Wabsolute-value, warnings about absolute value functions. The warnings fall into three groups. 1) Using an absolute value function of the wrong type, for instance, using the int absolute value function when the argument is a floating point type. 2) Using the improper sized absolute value function, for instance, using abs when the argument is a long long. llabs should be used instead. From these two cases, an implicit conversion will occur which may cause unexpected behavior. Where possible, suggest the proper absolute value function to use, and which header to include if the function is not available. 3) Taking the absolute value of an unsigned value. In addition to this warning, suggest to remove the function call. This usually indicates a logic error since the programmer assumed negative values would have been possible. llvm-svn: 202211
Used in Clang Sources
This section lists all occurrences of the diagnostic within the Clang's codebase. For each occurrence, an auto-extracted snipped from the source code is listed including key elements like control structures, functions, or classes. It should illustrate the conditions under which the diagnostic is activated.
clang/lib/Sema/SemaChecking.cpp (line 12024)
// Warn when using the wrong abs() function.
void Sema::CheckAbsoluteValueFunction(const CallExpr *Call, const FunctionDecl *FDecl) {
// ...
// Unsigned types cannot be negative. Suggest removing the absolute value
// function call.
if (ArgType->isUnsignedIntegerType()) {
// ...
Diag(Call->getExprLoc(), diag::warn_unsigned_abs) << ArgType << ParamType;
clang/utils/TableGen/ClangDiagnosticsEmitter.cpp (line 1409)
/// diag::warn_unsigned_abs,
Triggered in Clang Tests
This section lists all internal Clang test cases that trigger the diagnostic.
clang/test/Sema/warn-absolute-value-header.c
- clang/test/Sema/warn-absolute-value-header.c:15:9: warning: taking the absolute value of unsigned type 'unsigned int' has no effect [-Wabsolute-value]