[clang-tidy][readability-identifier-length] refactoring and cleanup (#194610)
This PR implements the refactorings discussed with @localspook in #193838 --------- Co-authored-by: Victor Chernyakin <chernyakin.victor.j@outlook.com>
This commit is contained in:
@@ -138,99 +138,53 @@ static bool isShortLived(const ValueDecl *Var, const SourceManager *SrcMgr,
|
||||
}
|
||||
|
||||
void IdentifierLengthCheck::check(const MatchFinder::MatchResult &Result) {
|
||||
const auto *StandaloneVar =
|
||||
Result.Nodes.getNodeAs<ValueDecl>("standaloneVar");
|
||||
if (StandaloneVar) {
|
||||
if (!StandaloneVar->getIdentifier())
|
||||
auto WarnIfTooShort = [&](const ValueDecl *Var, unsigned MinNameLength,
|
||||
const llvm::Regex &IgnoredNames, unsigned VarKind) {
|
||||
if (!Var->getIdentifier())
|
||||
return;
|
||||
|
||||
const StringRef VarName = StandaloneVar->getName();
|
||||
|
||||
if (VarName.size() >= MinimumVariableNameLength ||
|
||||
IgnoredVariableNames.match(VarName))
|
||||
const StringRef VarName = Var->getName();
|
||||
if (VarName.size() >= MinNameLength || IgnoredNames.match(VarName))
|
||||
return;
|
||||
|
||||
if (isShortLived(StandaloneVar, Result.SourceManager, Result.Context,
|
||||
if (isShortLived(Var, Result.SourceManager, Result.Context,
|
||||
LineCountThreshold))
|
||||
return;
|
||||
|
||||
diag(StandaloneVar->getLocation(), ErrorMessage)
|
||||
<< 0 << StandaloneVar << MinimumVariableNameLength;
|
||||
diag(Var->getLocation(), ErrorMessage) << VarKind << Var << MinNameLength;
|
||||
};
|
||||
|
||||
if (const auto *StandaloneVar =
|
||||
Result.Nodes.getNodeAs<ValueDecl>("standaloneVar")) {
|
||||
WarnIfTooShort(StandaloneVar, MinimumVariableNameLength,
|
||||
IgnoredVariableNames, 0);
|
||||
return;
|
||||
}
|
||||
|
||||
const auto *BindingVar = Result.Nodes.getNodeAs<ValueDecl>("bindingVar");
|
||||
if (BindingVar) {
|
||||
if (!BindingVar->getIdentifier())
|
||||
return;
|
||||
|
||||
const StringRef VarName = BindingVar->getName();
|
||||
|
||||
if (VarName.size() >= MinimumBindingNameLength ||
|
||||
IgnoredBindingNames.match(VarName))
|
||||
return;
|
||||
|
||||
if (isShortLived(BindingVar, Result.SourceManager, Result.Context,
|
||||
LineCountThreshold))
|
||||
return;
|
||||
|
||||
diag(BindingVar->getLocation(), ErrorMessage)
|
||||
<< 1 << BindingVar << MinimumBindingNameLength;
|
||||
if (const auto *BindingVar =
|
||||
Result.Nodes.getNodeAs<ValueDecl>("bindingVar")) {
|
||||
WarnIfTooShort(BindingVar, MinimumBindingNameLength, IgnoredBindingNames,
|
||||
1);
|
||||
return;
|
||||
}
|
||||
|
||||
auto *ExceptionVarName = Result.Nodes.getNodeAs<ValueDecl>("exceptionVar");
|
||||
if (ExceptionVarName) {
|
||||
if (!ExceptionVarName->getIdentifier())
|
||||
return;
|
||||
|
||||
const StringRef VarName = ExceptionVarName->getName();
|
||||
if (VarName.size() >= MinimumExceptionNameLength ||
|
||||
IgnoredExceptionVariableNames.match(VarName))
|
||||
return;
|
||||
|
||||
if (isShortLived(ExceptionVarName, Result.SourceManager, Result.Context,
|
||||
LineCountThreshold))
|
||||
return;
|
||||
|
||||
diag(ExceptionVarName->getLocation(), ErrorMessage)
|
||||
<< 2 << ExceptionVarName << MinimumExceptionNameLength;
|
||||
if (const auto *ExceptionVar =
|
||||
Result.Nodes.getNodeAs<ValueDecl>("exceptionVar")) {
|
||||
WarnIfTooShort(ExceptionVar, MinimumExceptionNameLength,
|
||||
IgnoredExceptionVariableNames, 2);
|
||||
return;
|
||||
}
|
||||
|
||||
const auto *LoopVar = Result.Nodes.getNodeAs<ValueDecl>("loopVar");
|
||||
if (LoopVar) {
|
||||
if (!LoopVar->getIdentifier())
|
||||
return;
|
||||
|
||||
const StringRef VarName = LoopVar->getName();
|
||||
|
||||
if (VarName.size() >= MinimumLoopCounterNameLength ||
|
||||
IgnoredLoopCounterNames.match(VarName))
|
||||
return;
|
||||
|
||||
if (isShortLived(LoopVar, Result.SourceManager, Result.Context,
|
||||
LineCountThreshold))
|
||||
return;
|
||||
|
||||
diag(LoopVar->getLocation(), ErrorMessage)
|
||||
<< 3 << LoopVar << MinimumLoopCounterNameLength;
|
||||
if (const auto *LoopVar = Result.Nodes.getNodeAs<ValueDecl>("loopVar")) {
|
||||
WarnIfTooShort(LoopVar, MinimumLoopCounterNameLength,
|
||||
IgnoredLoopCounterNames, 3);
|
||||
return;
|
||||
}
|
||||
|
||||
const auto *ParamVar = Result.Nodes.getNodeAs<ValueDecl>("paramVar");
|
||||
if (ParamVar) {
|
||||
if (!ParamVar->getIdentifier())
|
||||
return;
|
||||
|
||||
const StringRef VarName = ParamVar->getName();
|
||||
|
||||
if (VarName.size() >= MinimumParameterNameLength ||
|
||||
IgnoredParameterNames.match(VarName))
|
||||
return;
|
||||
|
||||
if (isShortLived(ParamVar, Result.SourceManager, Result.Context,
|
||||
LineCountThreshold))
|
||||
return;
|
||||
|
||||
diag(ParamVar->getLocation(), ErrorMessage)
|
||||
<< 4 << ParamVar << MinimumParameterNameLength;
|
||||
if (const auto *ParamVar = Result.Nodes.getNodeAs<ValueDecl>("paramVar")) {
|
||||
WarnIfTooShort(ParamVar, MinimumParameterNameLength, IgnoredParameterNames,
|
||||
4);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -32,19 +32,19 @@ private:
|
||||
const unsigned MinimumExceptionNameLength;
|
||||
const unsigned MinimumParameterNameLength;
|
||||
|
||||
std::string IgnoredVariableNamesInput;
|
||||
StringRef IgnoredVariableNamesInput;
|
||||
llvm::Regex IgnoredVariableNames;
|
||||
|
||||
std::string IgnoredBindingNamesInput;
|
||||
StringRef IgnoredBindingNamesInput;
|
||||
llvm::Regex IgnoredBindingNames;
|
||||
|
||||
std::string IgnoredLoopCounterNamesInput;
|
||||
StringRef IgnoredLoopCounterNamesInput;
|
||||
llvm::Regex IgnoredLoopCounterNames;
|
||||
|
||||
std::string IgnoredExceptionVariableNamesInput;
|
||||
StringRef IgnoredExceptionVariableNamesInput;
|
||||
llvm::Regex IgnoredExceptionVariableNames;
|
||||
|
||||
std::string IgnoredParameterNamesInput;
|
||||
StringRef IgnoredParameterNamesInput;
|
||||
llvm::Regex IgnoredParameterNames;
|
||||
|
||||
const unsigned LineCountThreshold;
|
||||
|
||||
@@ -4,9 +4,9 @@ readability-identifier-length
|
||||
=============================
|
||||
|
||||
This check finds variables and function parameters whose length are too short.
|
||||
The desired name length is configurable.
|
||||
|
||||
Special cases are supported for loop counters and for exception variable names.
|
||||
The desired name length is configurable. Special short names which should be
|
||||
ignored can be specified. Local variables with short names can also be ignored
|
||||
if they are short-lived.
|
||||
|
||||
Options
|
||||
-------
|
||||
@@ -28,14 +28,10 @@ The following options are described below:
|
||||
`MinimumVariableNameLength` (default is `3`). Setting it to `0` or `1`
|
||||
disables the check entirely.
|
||||
|
||||
|
||||
.. code-block:: c++
|
||||
|
||||
int i = 42; // warns that 'i' is too short
|
||||
|
||||
This check does not have any fix suggestions in the general case since
|
||||
variable names have semantic value.
|
||||
|
||||
.. option:: IgnoredVariableNames
|
||||
|
||||
Specifies a regular expression for variable names that are
|
||||
@@ -51,9 +47,6 @@ The following options are described below:
|
||||
|
||||
auto [a] = get_result(); // warns that 'a' is too short
|
||||
|
||||
This check does not have any fix suggestions in the general case since
|
||||
variable names have semantic value.
|
||||
|
||||
.. option:: IgnoredBindingNames
|
||||
|
||||
Specifies a regular expression for variable names introduced by structured
|
||||
@@ -66,7 +59,6 @@ The following options are described below:
|
||||
`MinimumParameterNameLength` (default is `3`). Setting it to `0` or `1`
|
||||
disables the check entirely.
|
||||
|
||||
|
||||
.. code-block:: c++
|
||||
|
||||
int doubler(int x) // warns that x is too short
|
||||
@@ -74,9 +66,6 @@ The following options are described below:
|
||||
return 2 * x;
|
||||
}
|
||||
|
||||
This check does not have any fix suggestions in the general case since
|
||||
variable names have semantic value.
|
||||
|
||||
.. option:: IgnoredParameterNames
|
||||
|
||||
Specifies a regular expression for parameters that are to be ignored.
|
||||
@@ -88,7 +77,6 @@ The following options are described below:
|
||||
`MinimumLoopCounterNameLength` characters (default is `2`). Setting it to
|
||||
`0` or `1` disables the check entirely.
|
||||
|
||||
|
||||
.. code-block:: c++
|
||||
|
||||
// This warns that 'q' is too short.
|
||||
@@ -103,7 +91,6 @@ The following options are described below:
|
||||
reasons and the last one since it is frequently used as a "don't care"
|
||||
value, specifically in tools such as Google Benchmark.
|
||||
|
||||
|
||||
.. code-block:: c++
|
||||
|
||||
// This does not warn by default, for historical reasons.
|
||||
@@ -117,7 +104,6 @@ The following options are described below:
|
||||
`MinimumExceptionNameLength` (default is `2`). Setting it to `0` or `1`
|
||||
disables the check entirely.
|
||||
|
||||
|
||||
.. code-block:: c++
|
||||
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user