| #include <expected>
|
| #include <cmath>
|
|
|
| // for printf to not get warnings about std::print throwing...
|
| #include <cstdio>
|
|
|
| std::expected<int, int> squareRoot(int value)
|
| {
|
| if (value < 0)
|
| {
|
| return std::unexpected{0};
|
| } else {
|
| return std::sqrt(value);
|
| }
|
| }
|
|
|
| // triggers false positive
|
| void sillySquareRootNoReturn(int value)
|
| {
|
| auto valueResult = squareRoot(value);
|
| if (not valueResult)
|
| {
|
| // NOLINTNEXTLINE(modernize-use-std-print)
|
| printf("Error: invalid value %d\n", value);
|
| return;
|
| }
|
|
|
| auto root = valueResult.value();
|
|
|
| // NOLINTNEXTLINE(modernize-use-std-print)
|
| printf("the square root of %d is %d\n", value, root);
|
| }
|
|
|
| int main() {
|
| int value = -9;
|
|
|
| sillySquareRootNoReturn(value);
|
|
|
| return 0;
|
| }
|
| #include <expected>
|
| #include <cmath>
|
|
|
| // for printf to not get warnings about std::print throwing...
|
| #include <cstdio>
|
|
|
| std::expected<int, int> squareRoot(int value)
|
| {
|
| if (value < 0)
|
| {
|
| return std::unexpected{0};
|
| } else {
|
| return std::sqrt(value);
|
| }
|
| }
|
|
|
| // does not trigger false positive
|
| int sillySquareRootWithReturn(int value)
|
| {
|
| auto valueResult = squareRoot(value);
|
| if (not valueResult)
|
| {
|
| // NOLINTNEXTLINE(modernize-use-std-print)
|
| printf("Error: invalid value %d\n", value);
|
| return 0;
|
| }
|
|
|
| return valueResult.value();
|
| }
|
|
|
| int main() {
|
| int value = -9;
|
|
|
| // NOLINTNEXTLINE(modernize-use-std-print)
|
| printf("the square root of %d is %d\n", value, sillySquareRootWithReturn(value));
|
|
|
| return 0;
|
| }
|
| project(
|
| 'false_positive',
|
| 'cpp',
|
| meson_version : '>= 1.3.0',
|
| version : '0.1',
|
| default_options : ['warning_level=3', 'cpp_std=c++23'],
|
| license : 'MIT',
|
| )
|
|
|
| false_positive = executable(
|
| 'false_positive',
|
| ['false_positive.cpp'],
|
| dependencies : [],
|
| install : true,
|
| )
|
|
|
| test('basic', false_positive)
|
|
|
| false_positive = executable(
|
| 'no_false_positive',
|
| ['no_false_positive.cpp'],
|
| dependencies : [],
|
| install : true,
|
| )
|
|
|
| test('basic', false_positive)
|
| Running clang-tidy for 2 files out of 2 in compilation database ...
|
| [1/2][0.9s] /usr/lib/llvm/21/bin/clang-tidy -p=build/ /home/libfud/Documents/demos/dumb_clang_tidy/false_positive.cpp
|
| ../false_positive.cpp:34:5: warning: an exception may be thrown in function 'main' which should not throw exceptions [bugprone-exception-escape]
|
| 34 | int main() {
|
| | ^
|
| /usr/lib/gcc/x86_64-pc-linux-gnu/15/include/g++-v15/expected:774:2: note: frame #0: unhandled exception of type 'std::bad_expected_access<int>' may be thrown in function 'value' here
|
| 774 | _GLIBCXX_THROW_OR_ABORT(bad_expected_access<_Er>(__unex));
|
| | ^
|
| /usr/lib/gcc/x86_64-pc-linux-gnu/15/include/g++-v15/x86_64-pc-linux-gnu/bits/c++config.h:262:42: note: expanded from macro '_GLIBCXX_THROW_OR_ABORT'
|
| 262 | # define _GLIBCXX_THROW_OR_ABORT(_EXC) (throw (_EXC))
|
| | ^
|
| ../false_positive.cpp:28:17: note: frame #1: function 'sillySquareRootNoReturn' calls function 'value' here
|
| 28 | auto root = valueResult.value();
|
| | ^
|
| ../false_positive.cpp:37:5: note: frame #2: function 'main' calls function 'sillySquareRootNoReturn' here
|
| 37 | sillySquareRootNoReturn(value);
|
| | ^
|
| 11744 warnings generated.
|
| Suppressed 11743 warnings (11743 in non-user code).
|
| Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.
|
|
|
| [2/2][0.9s] /usr/lib/llvm/21/bin/clang-tidy -p=build/ /home/libfud/Documents/demos/dumb_clang_tidy/no_false_positive.cpp
|
| 11743 warnings generated.
|
| Suppressed 11743 warnings (11743 in non-user code).
|
| Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.
|
| ---
|
| Checks: 'clang-diagnostic-*,clang-analyzer-*,bugprone*'
|
| WarningsAsErrors: ''
|
| HeaderFileExtensions:
|
| - ''
|
| - h
|
| - hh
|
| - hpp
|
| - hxx
|
| ImplementationFileExtensions:
|
| - c
|
| - cc
|
| - cpp
|
| - cxx
|
| HeaderFilterRegex: ''
|
| FormatStyle: none
|
| CheckOptions:
|
| cert-dcl16-c.NewSuffixes: 'L;LL;LU;LLU'
|
| SystemHeaders: false
|
| ...
|