llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

False positives in `modernize-use-default-member-init` in clang-tidy 21

Open arvid-norlander opened this issue 1 year ago • 3 comments

The release notes for clang-tidy 21 reads:

Improved modernize-use-default-member-init check by matching arithmetic operations, constexpr and static values, and detecting explicit casting of built-in types within member list initialization.

However there seem to be some false positives when there is an expression that partly depends on a constructor argument. E.g.:

#include <cstdint>

constexpr double CONSTANT = 2.0;

class Example1
{
   explicit Example1(const int32_t param)
   : member(
        static_cast<int32_t>(CONSTANT / (static_cast<double>(param) * 0.001)))
   {
   }

   const int32_t member;
};

This results in:

test.cpp:13:18: error: use default member initializer for 'member' [modernize-use-default-member-init,-warnings-as-errors]
    8 |    : member(
      |      ~~~~~~~
    9 |         static_cast<int32_t>(CONSTANT / (static_cast<double>(param) * 0.001)))
      |         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   10 |    {
   11 |    }
   12 | 
   13 |    const int32_t member;
      |                  ^     

Using -fix creates the invalid code below:

#include <cstdint>

constexpr double CONSTANT = 2.0;

class Example1
{
   explicit Example1(const int32_t param)
    
   {
   }

   const int32_t member{
        static_cast<int32_t>(CONSTANT / (static_cast<double>(param) * 0.001))};
};

arvid-norlander avatar Sep 01 '25 08:09 arvid-norlander

@llvm/issue-subscribers-clang-tidy

Author: Arvid Norlander (arvid-norlander)

The release notes for [clang-tidy 21](https://releases.llvm.org/21.1.0/tools/clang/tools/extra/docs/ReleaseNotes.html#improvements-to-clang-tidy) reads: > Improved [modernize-use-default-member-init](https://releases.llvm.org/21.1.0/tools/clang/tools/extra/docs/clang-tidy/checks/modernize/use-default-member-init.html) check by matching arithmetic operations, constexpr and static values, and detecting explicit casting of built-in types within member list initialization.

However there seem to be some false positives when there is an expression that partly depends on a constructor argument. E.g.:

#include &lt;cstdint&gt;

constexpr double CONSTANT = 2.0;

class Example1
{
   explicit Example1(const int32_t param)
   : member(
        static_cast&lt;int32_t&gt;(CONSTANT / (static_cast&lt;double&gt;(param) * 0.001)))
   {
   }

   const int32_t member;
};

This results in:

test.cpp:13:18: error: use default member initializer for 'member' [modernize-use-default-member-init,-warnings-as-errors]
    8 |    : member(
      |      ~~~~~~~
    9 |         static_cast&lt;int32_t&gt;(CONSTANT / (static_cast&lt;double&gt;(param) * 0.001)))
      |         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   10 |    {
   11 |    }
   12 | 
   13 |    const int32_t member;
      |                  ^     

Using -fix creates the invalid code below:

#include &lt;cstdint&gt;

constexpr double CONSTANT = 2.0;

class Example1
{
   explicit Example1(const int32_t param)
    
   {
   }

   const int32_t member{
        static_cast&lt;int32_t&gt;(CONSTANT / (static_cast&lt;double&gt;(param) * 0.001))};
};

llvmbot avatar Sep 01 '25 08:09 llvmbot

CC @RiverDave

vbvictor avatar Sep 01 '25 16:09 vbvictor

CC @RiverDave

Hi @vbvictor thanks for bringing this up. School has been keeping me busy, and there's no way for me to fix the issues on this check in the short term. I hope I can put some work on these checks by the end of the semester.

RiverDave avatar Sep 22 '25 12:09 RiverDave