首页 > 解决方案 > clang-tidy:什么可能导致 NOLINT 评论不被尊重?

问题描述

我为开源交通服务器项目创建了一个 PR。作为 CI 的一部分,他们运行得很整洁。我的更改向 clang-tidy 公开了一个新文件,该文件现在被标记为移动后使用警告。这是测试代码,固定在我的更改所基于的提交处:

https://github.com/apache/trafficserver/blob/415cd8f/tests/tools/plugins/test_cppapi.cc#L115

这是该代码的副本,并添加了一条注释,显示了它抱怨的地方:

void
f()
{
  TestCont::Mutex m(TSMutexCreate());

  TestCont c(m);

  ALWAYS_ASSERT(!!c)
  ALWAYS_ASSERT(c.asTSCont() != nullptr)
  ALWAYS_ASSERT(c.mutex() == m)

  TestCont c2(std::move(c));

  ALWAYS_ASSERT(!!c2)
  ALWAYS_ASSERT(c2.asTSCont() != nullptr)   // <--- Complains here
  ALWAYS_ASSERT(c2.mutex() == m)

  ALWAYS_ASSERT(!c)
  ALWAYS_ASSERT(c.asTSCont() == nullptr)
  ALWAYS_ASSERT(c.mutex() == nullptr)

所以抱怨是有道理的,c2 是在移动之后使用的。但在这种情况下,TestCont通过设计明确支持在移动后使用,并且测试有意执行此操作以确保其状态符合预期。

因此,这是一种为之而NOLINT生的情况NOLINTNEXTLINE。所以我应用了这样的评论(显然我添加的评论比我绝望的需要更多):

  // NOLINTNEXTLINE
   ALWAYS_ASSERT(!c) // NOLINT
   // We turn off the clang-tidy warning about this being a use after move
   // because that is the intention of the test. Continuations support use after
   // move.
   // NOLINTNEXTLINE
   const auto cont_after_move = c.asTSCont(); // NOLINT
   ALWAYS_ASSERT(cont_after_move == nullptr)
   // We turn off the clang-tidy warning about this being a use after move
   // because that is the intention of the test. Continuations support use after
   // move.
   // NOLINTNEXTLINE
   const auto mutex_after_move = c.mutex(); // NOLINT
   ALWAYS_ASSERT(mutex_after_move == nullptr)

请注意,我将c.asTSCont()其作为单独的调用分开,以防ALWAYS_ASSERT宏混淆了事情。然而,clang-tidy 仍然在抱怨。这是最新的詹金斯运行输出:

https://ci.trafficserver.apache.org/job/clang-analyzer-github/12245/console

tools/plugins/test_cppapi.cc:124:32: warning: Method called on moved-from object 'c'
  const auto cont_after_move = c.asTSCont(); // NOLINT
                               ^~~~~~~~~~~~
tools/plugins/test_cppapi.cc:151:33: warning: Method called on moved-from object 'c2'
  const auto cont2_after_move = c2.asTSCont(); // NOLINT
                                ^~~~~~~~~~~~~
2 warnings generated.

就在警告输出中是NOLINT注释。我究竟做错了什么?为什么 clang-tidy 不尊重NOLINT

clang-tidy 版本是 10.0.0。以下是 clang-analyzer 报告,以防万一:

https://ci.trafficserver.apache.org/clang-analyzer/github/6945/2020-06-25-081635-12865-1/report-33708c.html#EndPath

这是 clang-tidy 调用的复制和粘贴:

clang -cc1 -triple x86_64-unknown-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name test_cppapi.cc -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -mthread-model posix -mframe-pointer=none -relaxed-aliasing -fmath-errno -fno-rounding-math -masm-verbose -mconstructor-aliases -munwind-tables -target-cpu x86-64 -target-feature +cx16 -dwarf-column-info -fno-split-dwarf-inlining -debugger-tuning=gdb -resource-dir /opt/llvm/lib64/clang/10.0.0 -D HAVE_CONFIG_H -I . -I ../include -D linux -D _LARGEFILE64_SOURCE=1 -D _COMPILE64BIT_SOURCE=1 -D _REENTRANT -D __STDC_LIMIT_MACROS=1 -D __STDC_FORMAT_MACROS=1 -I /var/jenkins/workspace/clang-analyzer-github/src/proxy/api -I /var/jenkins/workspace/clang-analyzer-github/src/proxy/api -I /var/jenkins/workspace/clang-analyzer-github/src/include/cppapi/include -I /var/jenkins/workspace/clang-analyzer-github/src/lib/cppapi/include -I /var/jenkins/workspace/clang-analyzer-github/src/include -I /var/jenkins/workspace/clang-analyzer-github/src/lib -D _GNU_SOURCE -D OPENSSL_NO_SSL_INTERN -I /opt/llvm/include/c++/v1 -D PIC -internal-isystem /opt/llvm/bin/../include/c++/v1 -internal-isystem /usr/local/include -internal-isystem /opt/llvm/lib64/clang/10.0.0/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -O3 -Wno-deprecated-declarations -Wno-ignored-qualifiers -Wno-unused-parameter -Wno-invalid-offsetof -std=c++17 -fdeprecated-macro -fdebug-compilation-dir /var/jenkins/workspace/clang-analyzer-github/src/tests -ferror-limit 19 -fmessage-length 0 -fgnuc-version=4.2.1 -fobjc-runtime=gcc -fcxx-exceptions -fexceptions -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-checker alpha.unix.cstring.BufferOverlap -analyzer-checker alpha.core.BoolAssignment -analyzer-checker alpha.core.CastSize -analyzer-checker alpha.core.SizeofPtr -analyzer-output=html -faddrsig -o /CA/clang-analyzer/github/6945/2020-06-25-081635-12865-1 -x c++ tools/plugins/test_cppapi.cc

标签: c++clang-tidy

解决方案


最后,我在 clang-tidy 和 clang-analyzer 之间感到困惑。NOLINT 解决了 clang-tidy 问题,但我不得不抑制 clang-analyzer。我这样做是使用这里的建议:

https://clang-analyzer.llvm.org/faq.html#exclude_code

以下指令为我消除了警告:

#ifndef __clang_analyzer__
// Code not to be analyzed
#endif

推荐阅读