Search code examples
clangclang-tidy

In clang-tidy, how to set a check option that accepts at list of numbers


I am using clang-tidy with a .clang-tidy configuration file. The file is read correctly, and I am able to set any kind of check options, except for check options that take a list of numbers as a value.

Here is my .clang-tidy file, which attempts to set the check options modernize-use-nullptr.NullMacros and readability-magic-numbers.IgnoredIntegerValues:

Checks: 'modernize-use-nullptr,readability-magic-numbers'
CheckOptions:
  - key:             modernize-use-nullptr.NullMacros
    value:           IT_WORKS_WITH_A_SIMPLE_VALUE
  - key:             readability-magic-numbers.IgnoredIntegerValues
    value:           '5;6;7;8;'

When I run clang-tidy with the --dump-config option, I get the following result:

---
Checks:          'clang-diagnostic-*,clang-analyzer-*,modernize-use-nullptr,readability-magic-numbers'
WarningsAsErrors: ''
HeaderFilterRegex: ''
AnalyzeTemporaryDtors: false
FormatStyle:     none
User:            user
CheckOptions:
  - key:             cert-dcl16-c.NewSuffixes
    value:           'L;LL;LU;LLU'
  - key:             cert-oop54-cpp.WarnOnlyIfThisHasSuspiciousField
    value:           '0'
  - key:             cppcoreguidelines-explicit-virtual-functions.IgnoreDestructors
    value:           '1'
  - key:             cppcoreguidelines-non-private-member-variables-in-classes.IgnoreClassesWithAllMemberVariablesBeingPublic
    value:           '1'
  - key:             google-readability-braces-around-statements.ShortStatementLines
    value:           '1'
  - key:             google-readability-function-size.StatementThreshold
    value:           '800'
  - key:             google-readability-namespace-comments.ShortNamespaceLines
    value:           '10'
  - key:             google-readability-namespace-comments.SpacesBeforeComments
    value:           '2'
  - key:             modernize-loop-convert.MaxCopySize
    value:           '16'
  - key:             modernize-loop-convert.MinConfidence
    value:           reasonable
  - key:             modernize-loop-convert.NamingStyle
    value:           CamelCase
  - key:             modernize-pass-by-value.IncludeStyle
    value:           llvm
  - key:             modernize-replace-auto-ptr.IncludeStyle
    value:           llvm
  - key:             modernize-use-nullptr.NullMacros
    value:           IT_WORKS_WITH_A_SIMPLE_VALUE
  - key:             readability-magic-numbers.IgnoredFloatingPointValues
    value:           '1.0;100.0;'
  - key:             readability-magic-numbers.IgnoredIntegerValues
    value:           '1;2;3;4;'
...

As you can see, the modernize-use-nullptr.NullMacros check option was set correctly, but the readability-magic-numbers.IgnoredIntegerValues check option wasn't set.

I think it is probably a problem of syntax, but I use the same syntax as the one given by --dump-config, which is supposed to be the right syntax according to clang-tidy documentation.

How can I set the readability-magic-numbers.IgnoredIntegerValues check option?

Running clang-tidy --version gives the following result:

LLVM (http://llvm.org/):
  LLVM version 9.0.1
  Optimized build.
  Default target: x86_64-pc-linux-gnu
  Host CPU: ivybridge

Solution

  • This seems like a bug in the --dump-config for readability-magic-numbers check only. Your clang-tidy file works just fine as far as I can tell.

    I ran clang-tidy with your .clang-tidy file on this sample code:

    int badGlobalInt = 5;
    
    int main()
    {
        int badLocalInt = 8;
        int unfilteredBadLocalInt = 9;
        return 0;
    }
    

    Command line:

    N:\xxx>clang-tidy.exe xxx.cpp --
    

    The result was just as expected:

    1 warning generated.
    N:\xxx\xxx.cpp:6:30: warning: 9 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
            int unfilteredBadLocalInt = 9;
                                        ^
    

    The results were exactly the same for clang-tidy 9 and 10.