Clang warning: taking the absolute value of unsigned type A has no effect [-Wabsolute-value] (warn_unsigned_abs)

From emmtrix Wiki
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

The warning is issued by the Clang compiler when the abs function or similar mechanisms are used to compute the absolute value of an unsigned integer type. Since unsigned integers can only represent non-negative values, attempting to take the absolute value of such types is redundant and indicates a logical error in the code. This scenario commonly arises when a programmer incorrectly assumes that an unsigned variable might hold negative values and uses the abs function to ensure positivity. However, this operation has no effect on unsigned types, as they are already guaranteed to be non-negative. The compiler generates this warning to alert the developer of the unnecessary use of the absolute value function on an unsigned type and usually suggests removing the call to abs entirely. The aim is to prompt a review of the logic involving unsigned variables, potentially uncovering and correcting misunderstandings or misuses in the code base.  
AI Generated

Example

In the following example, an unsigned integer variable is defined with an initial value. The abs function is then called with this unsigned variable as its argument in an attempt to obtain the absolute value. However, as unsigned integers can only represent non-negative values, the call to abs is redundant. The compiler warning indicates this redundancy by stating that taking the absolute value of an unsigned type has no effect and suggests removing the call to abs.  
AI Generated


Flags -xc

[Try out in Compiler Explorer]

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]