Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clang-tidy readability-identifier-naming module does not seem to properly handle class attributes and class methods

Tags:

clang-tidy

I would like to use clang-tidy 'readability-identifier-naming' module to clean my code, but I failed to use it properly on a short example with class attribute and method.

I used the following .clang-tidy file:

Checks: '-*,readability-identifier-naming'
CheckOptions:
  - { key: readability-identifier-naming.ClassCase,     value: CamelCase  }
  - { key: readability-identifier-naming.VariableCase,  value: lower_case }
  - { key: readability-identifier-naming.FunctionCase,  value: lower_case }
  - { key: readability-identifier-naming.MemberPrefix,  value: m_         }
  - { key: readability-identifier-naming.ParameterCase, value: lower_case }

on this code:

class one_class
{
public:
    int OneMethod(int OneArgument);

    int OneAttribute;
};

int one_class::OneMethod(int OneArgument)
{
    OneAttribute = 42;
    return OneArgument + 1;
}

int main(void)
{
    int OneVariable = 0;

    one_class c;
    OneVariable = c.OneMethod(OneVariable);
    c.OneAttribute = 21;

    return 0;
}

The result is this code:

class OneClass
{
public:
    int one_method(int one_argument);

    int m_OneAttribute;
};

int OneClass::one_method(int one_argument)
{
    OneAttribute = 42; // must be m_OneAttribute =
    return one_argument + 1;
}

int main(void)
{
    int one_variable = 0;

    OneClass c;
    one_variable = c.OneMethod(one_variable); // must be c.one_method(...)
    c.OneAttribute = 21; // must be c.m_OneAttribute = ...

    return 0;
}

The declaration and the definition of the class method OneMethod() has been properly transformed, BUT the method call in the main() function has NOT. The same for the class attribute OneAttribute. The resulting code doesn't compile anymore.

I called clang-tidy with this command line :

clang-tidy-5.0  -checks='readability-identifier-naming'  -fix  test.cpp  --

I have clang 5.0 and clang 3.8 installed from packages on an Ubuntu 16.04.

What am I doing wrong ?

like image 859
devel484 Avatar asked Jun 18 '18 09:06

devel484


1 Answers

You aren't doing anything wrong. clang-tidy just doesn't cover this case for some reason. It may just be a bug, though it seems quite a glaring one if it is.

Interestingly clang-rename can do such renames correctly, and shows that clang-tidy's failure is not because of technical feasibility.

It might be worth filing a bug about this here: https://bugs.llvm.org/enter_bug.cgi?product=clang-tools-extra

Though there's a related bug that you should cc your self on for more attention to the issue

https://bugs.llvm.org/show_bug.cgi?id=41122

Looks like it may have been fixed in 2020-01:

https://bugs.llvm.org/show_bug.cgi?id=41122#c2

But that probably means Clang version < 11 will not have the fix unless it's backported.

like image 131
Catskul Avatar answered Oct 23 '22 03:10

Catskul