Closed
Description
In C++ attribute: likely
, unlikely
(since C++20), we can add [[likely]]
/ [[unlikely]]
on if-else
clauses. But cpplint
does not recognize the [[likely]]
/ [[unlikely]]
attributes after if
/ else
/ else if
:
Source file:
// foo.cpp
// Copyright 2022 <test>
#include <iostream>
int fib1(int n) {
if (n > 1) [[likely]] {
int a;
return fib1(n - 1) + fib1(n - 2);
} else if (n == 2) [[unlikely]] {
int b;
return 1;
} else [[unlikely]] {
if (n == 1) [[likely]]
return 1;
else [[unlikely]]
return 0;
}
}
int fib2(int n) {
switch (n) {
[[unlikely]] case 0: return 0;
case 1:
[[fallthrough]];
case 2:
[[fallthrough]];
[[unlikely]] case 3: {
return 1;
}
[[likely]] default:
return fib2(n - 1) + fib2(n - 2);
}
}
Command-line:
$ pip3 install -I git+https://github.com/cpplint/cpplint.git
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting git+https://github.com/cpplint/cpplint.git
Cloning https://github.com/cpplint/cpplint.git to /tmp/pip-req-build-6nkph3fd
Running command git clone --filter=blob:none --quiet https://github.com/cpplint/cpplint.git /tmp/pip-req-build-6nkph3fd
Resolved https://github.com/cpplint/cpplint.git to commit fa12a0bbdafa15291276ddd2a2dcd2ac7a2ce4cb
Preparing metadata (setup.py) ... done
Building wheels for collected packages: cpplint
Building wheel for cpplint (setup.py) ... done
Created wheel for cpplint: filename=cpplint-1.6.1-py3-none-any.whl size=77266 sha256=3e95bbfd3de2b8d61d561339213e6a4ae741e8bb110b34c9b6f626c55cd325c9
Stored in directory: /tmp/pip-ephem-wheel-cache-d42ge4uo/wheels/af/90/88/663d23197700a259f2a9199aef67ee9977e21f6d1980257857
Successfully built cpplint
Installing collected packages: cpplint
Successfully installed cpplint-1.6.1
$ cpplint foo.cpp
foo.cpp:7: If/else bodies with multiple statements require braces [readability/braces] [4]
foo.cpp:10: If/else bodies with multiple statements require braces [readability/braces] [4]
foo.cpp:13: Else clause should never be on same line as else (use 2 lines) [whitespace/newline] [4]
foo.cpp:13: If/else bodies with multiple statements require braces [readability/braces] [4]
foo.cpp:16: Else clause should never be on same line as else (use 2 lines) [whitespace/newline] [4]
Done processing foo.cpp
Total errors found: 5
4818
div>