From a7001564a22f9807119efb7b8f4cf6f74d4c12fc Mon Sep 17 00:00:00 2001 From: Boming Zhang Date: Sat, 27 Apr 2024 15:05:43 -0400 Subject: [PATCH] feat: init --- README.md | 3 + conf.toml | 26 +++++ expected.json | 1 + src/sillycode.cpp | 262 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 292 insertions(+) create mode 100644 README.md create mode 100644 conf.toml create mode 100644 expected.json create mode 100644 src/sillycode.cpp diff --git a/README.md b/README.md new file mode 100644 index 0000000..205fc8d --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# cpplint sillycode + +From . diff --git a/conf.toml b/conf.toml new file mode 100644 index 0000000..4d3583a --- /dev/null +++ b/conf.toml @@ -0,0 +1,26 @@ +skipGitea = true +[[stages]] +name = "cpplint" +[stages.executor] +name = "sandbox" +[stages.executor.with.default] +args = ["bash", "-c", "cpplint src/*.cpp"] +env = ["PATH=/usr/bin:/bin:/usr/local/bin"] +cpuLimit = 10_000_000_000 +memoryLimit = 104_857_600 +procLimit = 50 +copyInCwd = true +copyOut = ["stdout", "stderr"] +[stages.executor.with.default.stdin] +content = "" +[stages.executor.with.default.stdout] +name = "stdout" +max = 65_536 +[stages.executor.with.default.stderr] +name = "stderr" +max = 65_536 +[stages.parser] +name = "cpplint" +[stages.parser.with] +score = 100 +comment = "check done" diff --git a/expected.json b/expected.json new file mode 100644 index 0000000..d2bd6d0 --- /dev/null +++ b/expected.json @@ -0,0 +1 @@ +[{"name":"cpplint","results":[{"score":-262,"comment":"src/sillycode.cpp:0: No copyright message found. You should have a line: \"Copyright [year] \u003cCopyright Owner\u003e\" [legal/copyright] [5]\nsrc/sillycode.cpp:3: Found C system header after C++ system header. Should be: sillycode.h, c system, c++ system, other. [build/include_order] [4]\nsrc/sillycode.cpp:8: public: should be indented +1 space inside class Date [whitespace/indent] [3]\nsrc/sillycode.cpp:14: Is this a non-const reference? If so, make const or use a pointer: vector\u003cstring\u003e\u0026 v [runtime/references] [2]\nsrc/sillycode.cpp:15: { should almost always be at the end of the previous line [whitespace/braces] [4]\nsrc/sillycode.cpp:39: { should almost always be at the end of the previous line [whitespace/braces] [4]\nsrc/sillycode.cpp:40: Tab found; better to use spaces [whitespace/tab] [1]\nsrc/sillycode.cpp:40: If/else bodies with multiple statements require braces [readability/braces] [4]\nsrc/sillycode.cpp:49: Tab found; better to use spaces [whitespace/tab] [1]\nsrc/sillycode.cpp:49: At least two spaces is best between code and comments [whitespace/comments] [2]\nsrc/sillycode.cpp:55: { should almost always be at the end of the previous line [whitespace/braces] [4]\nsrc/sillycode.cpp:60: Tab found; better to use spaces [whitespace/tab] [1]\nsrc/sillycode.cpp:60: At least two spaces is best between code and comments [whitespace/comments] [2]\nsrc/sillycode.cpp:64: public: should be indented +1 space inside class X [whitespace/indent] [3]\nsrc/sillycode.cpp:65: Tab found; better to use spaces [whitespace/tab] [1]\nsrc/sillycode.cpp:65: At least two spaces is best between code and comments [whitespace/comments] [2]\nsrc/sillycode.cpp:66: Tab found; better to use spaces [whitespace/tab] [1]\nsrc/sillycode.cpp:66: Single-parameter constructors should be marked explicit. [runtime/explicit] [5]\nsrc/sillycode.cpp:74: public: should be indented +1 space inside class X2 [whitespace/indent] [3]\nsrc/sillycode.cpp:75: Tab found; better to use spaces [whitespace/tab] [1]\nsrc/sillycode.cpp:76: Tab found; better to use spaces [whitespace/tab] [1]\nsrc/sillycode.cpp:76: Single-parameter constructors should be marked explicit. [runtime/explicit] [5]\nsrc/sillycode.cpp:80: Tab found; better to use spaces [whitespace/tab] [1]\nsrc/sillycode.cpp:80: At least two spaces is best between code and comments [whitespace/comments] [2]\nsrc/sillycode.cpp:84: public: should be indented +1 space inside class X3 [whitespace/indent] [3]\nsrc/sillycode.cpp:85: Constructors callable with one argument should be marked explicit. [runtime/explicit] [5]\nsrc/sillycode.cpp:86: Tab found; better to use spaces [whitespace/tab] [1]\nsrc/sillycode.cpp:86: Lines should be \u003c= 80 characters long [whitespace/line_length] [2]\nsrc/sillycode.cpp:94: public: should be indented +1 space inside class Foo [whitespace/indent] [3]\nsrc/sillycode.cpp:100: { should almost always be at the end of the previous line [whitespace/braces] [4]\nsrc/sillycode.cpp:110: Tab found; better to use spaces [whitespace/tab] [1]\nsrc/sillycode.cpp:111: Tab found; better to use spaces [whitespace/tab] [1]\nsrc/sillycode.cpp:111: At least two spaces is best between code and comments [whitespace/comments] [2]\nsrc/sillycode.cpp:113: public: should be indented +1 space inside class Vector2 [whitespace/indent] [3]\nsrc/sillycode.cpp:118: Is this a non-const reference? If so, make const or use a pointer: N::X\u0026 a [runtime/references] [2]\nsrc/sillycode.cpp:118: Is this a non-const reference? If so, make const or use a pointer: N::X\u0026 b [runtime/references] [2]\nsrc/sillycode.cpp:119: { should almost always be at the end of the previous line [whitespace/braces] [4]\nsrc/sillycode.cpp:120: Tab found; better to use spaces [whitespace/tab] [1]\nsrc/sillycode.cpp:120: Missing space after , [whitespace/comma] [3]\nsrc/sillycode.cpp:122: Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3]\nsrc/sillycode.cpp:122: Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4]\nsrc/sillycode.cpp:123: Is this a non-const reference? If so, make const or use a pointer: N::X\u0026 a [runtime/references] [2]\nsrc/sillycode.cpp:123: Is this a non-const reference? If so, make const or use a pointer: N::X\u0026 b [runtime/references] [2]\nsrc/sillycode.cpp:124: { should almost always be at the end of the previous line [whitespace/braces] [4]\nsrc/sillycode.cpp:125: Tab found; better to use spaces [whitespace/tab] [1]\nsrc/sillycode.cpp:125: At least two spaces is best between code and comments [whitespace/comments] [2]\nsrc/sillycode.cpp:126: Tab found; better to use spaces [whitespace/tab] [1]\nsrc/sillycode.cpp:126: Missing space after , [whitespace/comma] [3]\nsrc/sillycode.cpp:143: Missing spaces around = [whitespace/operators] [4]\nsrc/sillycode.cpp:144: Missing spaces around = [whitespace/operators] [4]\nsrc/sillycode.cpp:149: Missing spaces around = [whitespace/operators] [4]\nsrc/sillycode.cpp:150: Missing spaces around = [whitespace/operators] [4]\nsrc/sillycode.cpp:155: Missing spaces around = [whitespace/operators] [4]\nsrc/sillycode.cpp:156: Missing spaces around = [whitespace/operators] [4]\nsrc/sillycode.cpp:162: At least two spaces is best between code and comments [whitespace/comments] [2]\nsrc/sillycode.cpp:164: Tab found; better to use spaces [whitespace/tab] [1]\nsrc/sillycode.cpp:166: At least two spaces is best between code and comments [whitespace/comments] [2]\nsrc/sillycode.cpp:168: At least two spaces is best between code and comments [whitespace/comments] [2]\nsrc/sillycode.cpp:171: Tab found; better to use spaces [whitespace/tab] [1]\nsrc/sillycode.cpp:171: Do not use variable-length arrays. Use an appropriately named ('k' followed by CamelCase) compile-time constant for the size. [runtime/arrays] [1]\nsrc/sillycode.cpp:175: Tab found; better to use spaces [whitespace/tab] [1]\nsrc/sillycode.cpp:178: Tab found; better to use spaces [whitespace/tab] [1]\nsrc/sillycode.cpp:178: At least two spaces is best between code and comments [whitespace/comments] [2]\nsrc/sillycode.cpp:178: Static/global string variables are not permitted. [runtime/string] [4]\nsrc/sillycode.cpp:179: Tab found; better to use spaces [whitespace/tab] [1]\nsrc/sillycode.cpp:179: At least two spaces is best between code and comments [whitespace/comments] [2]\nsrc/sillycode.cpp:184: Tab found; better to use spaces [whitespace/tab] [1]\nsrc/sillycode.cpp:184: At least two spaces is best between code and comments [whitespace/comments] [2]\nsrc/sillycode.cpp:190: Tab found; better to use spaces [whitespace/tab] [1]\nsrc/sillycode.cpp:190: At least two spaces is best between code and comments [whitespace/comments] [2]\nsrc/sillycode.cpp:195: Tab found; better to use spaces [whitespace/tab] [1]\nsrc/sillycode.cpp:195: At least two spaces is best between code and comments [whitespace/comments] [2]\nsrc/sillycode.cpp:197: Tab found; better to use spaces [whitespace/tab] [1]\nsrc/sillycode.cpp:197: At least two spaces is best between code and comments [whitespace/comments] [2]\nsrc/sillycode.cpp:199: If an else has a brace on one side, it should have it on both [readability/braces] [5]\nsrc/sillycode.cpp:202: An else should appear on the same line as the preceding } [whitespace/newline] [4]\nsrc/sillycode.cpp:202: If an else has a brace on one side, it should have it on both [readability/braces] [5]\nsrc/sillycode.cpp:208: Missing space before { [whitespace/braces] [5]\nsrc/sillycode.cpp:208: Static/global string variables are not permitted. [runtime/string] [4]\nsrc/sillycode.cpp:209: Tab found; better to use spaces [whitespace/tab] [1]\nsrc/sillycode.cpp:209: At least two spaces is best between code and comments [whitespace/comments] [2]\nsrc/sillycode.cpp:214: At least two spaces is best between code and comments [whitespace/comments] [2]\nsrc/sillycode.cpp:217: { should almost always be at the end of the previous line [whitespace/braces] [4]\nsrc/sillycode.cpp:218: Tab found; better to use spaces [whitespace/tab] [1]\nsrc/sillycode.cpp:218: At least two spaces is best between code and comments [whitespace/comments] [2]\nsrc/sillycode.cpp:219: Tab found; better to use spaces [whitespace/tab] [1]\nsrc/sillycode.cpp:219: At least two spaces is best between code and comments [whitespace/comments] [2]\nsrc/sillycode.cpp:220: Tab found; better to use spaces [whitespace/tab] [1]\nsrc/sillycode.cpp:220: At least two spaces is best between code and comments [whitespace/comments] [2]\nsrc/sillycode.cpp:225: Tab found; better to use spaces [whitespace/tab] [1]\nsrc/sillycode.cpp:225: At least two spaces is best between code and comments [whitespace/comments] [2]\nsrc/sillycode.cpp:227: Static/global string variables are not permitted. [runtime/string] [4]\nsrc/sillycode.cpp:228: At least two spaces is best between code and comments [whitespace/comments] [2]\nsrc/sillycode.cpp:228: Using C-style cast. Use reinterpret_cast\u003cdouble*\u003e(...) instead [readability/casting] [4]\nsrc/sillycode.cpp:236: public: should be indented +1 space inside class derived2 [whitespace/indent] [3]\nsrc/sillycode.cpp:241: At least two spaces is best between code and comments [whitespace/comments] [2]\nsrc/sillycode.cpp:243: Lines should be \u003c= 80 characters long [whitespace/line_length] [2]\nsrc/sillycode.cpp:243: At least two spaces is best between code and comments [whitespace/comments] [2]\nsrc/sillycode.cpp:243: Using C-style cast. Use reinterpret_cast\u003cderived2*\u003e(...) instead [readability/casting] [4]\nsrc/sillycode.cpp:244: Lines should be \u003c= 80 characters long [whitespace/line_length] [2]\nsrc/sillycode.cpp:244: At least two spaces is best between code and comments [whitespace/comments] [2]\nsrc/sillycode.cpp:253: At least two spaces is best between code and comments [whitespace/comments] [2]\nsrc/sillycode.cpp:254: At least two spaces is best between code and comments [whitespace/comments] [2]\nsrc/sillycode.cpp:257: Tab found; better to use spaces [whitespace/tab] [1]\nsrc/sillycode.cpp:257: At least two spaces is best between code and comments [whitespace/comments] [2]\nsrc/sillycode.cpp:260: Empty loop bodies should use {} or continue [whitespace/empty_loop_body] [5]\nsrc/sillycode.cpp:260: At least two spaces is best between code and comments [whitespace/comments] [2]\n"}]}] diff --git a/src/sillycode.cpp b/src/sillycode.cpp new file mode 100644 index 0000000..45c294d --- /dev/null +++ b/src/sillycode.cpp @@ -0,0 +1,262 @@ +#include + +#include + +#include +class Date { + // ... +public: + Month month() const; // do + int month(); // don't + // ... +}; + +void do_something(vector& v) +{ + string val; + cin >> val; + // ... + int index = 0; // bad + for (int i = 0; i < v.size(); ++i) + if (v[i] == val) { + index = i; + break; + } + // ... +} + +struct X { + char ch; + int i; + string s; + char ch2; + + X& operator=(const X& a); // NOLINT(clang-analyzer-valist.Uninitialized) + X(const X&); +}; + +X waste(const char* p) +{ + if (p == nullptr) throw Nullptr_error{}; + int n = strlen(p); + auto buf = new char[n]; + if (buf == nullptr) throw Allocation_error{}; + for (int i = 0; i < n; ++i) buf[i] = p[i]; + // ... manipulate buffer ... + X x; + x.ch = 'a'; + x.s = string(n); // give x.s space for *ps + for (int i = 0; i < x.s.size(); ++i) x.s[i] = buf[i]; // copy buf into x.s + delete buf; + return x; +} + +void driver() +{ + X x = waste("Typical argument"); + // ... +} + +class X { // BAD + int i; + string s; + int j; +public: + X() :i{666}, s{"qqq"} { } // j is uninitialized + X(int ii) :i{ii} {} // s is "" and j is uninitialized + // ... +}; + +class X2 { + int i {666}; + string s {"qqq"}; + int j {0}; +public: + X2() = default; // all members are initialized to their defaults + X2(int ii) :i{ii} {} // s and j initialized to their defaults + // ... +}; + +class X3 { // BAD: inexplicit, argument passing overhead + int i; + string s; + int j; +public: + X3(int ii = 666, const string& ss = "qqq", int jj = 0) + :i{ii}, s{ss}, j{jj} { } // all members are initialized to their defaults + // ... +}; + + +class Foo { + string s; + int i; +public: + Foo& operator=(Foo&& a); + // ... +}; + +Foo& Foo::operator=(Foo&& a) // OK, but there is a cost +{ + if (this == &a) return *this; // this line is redundant + s = std::move(a.s); + i = a.i; + return *this; +} + +template +class Vector2 { + // ... + Vector2(Vector2&& a) { *this = a; } // just use the copy + Vector2& operator=(Vector2&& a) { *this = a; } // just use the copy + //... +public: + T* elem; + int sz; +}; + +void f2(N::X& a, N::X& b) +{ + swap(a,b); // calls N::swap +} + +void f3(N::X& a, N::X& b) +{ + using std::swap; // make std::swap available + swap(a,b); // calls N::swap if it exists, otherwise std::swap +} + + +// webcolors.h (third party header) +#define RED 0xFF0000 +#define GREEN 0x00FF00 +#define BLUE 0x0000FF + +// productinfo.h +// The following define product subtypes based on color +#define RED 0 +#define PURPLE 1 +#define BLUE 2 + +int webby = BLUE; // webby==2; probably not what was desired + +enum class Webcolor { red=0xFF0000, green=0x00FF00, blue=0x0000FF }; +enum class Productinfo { red=0, purple=1, blue=2 }; + +int webby = blue; // error: be specific +Webcolor webby = Webcolor::blue; + +enum Webcolor { red=0xFF0000, green=0x00FF00, blue=0x0000FF }; +enum Productinfo { red=0, purple=1, blue=2 }; + +int webby = blue; // error, ambiguous: be specific +Webcolor webby = Webcolor::blue; + +enum class Webcolor { red=0xFF0000, green=0x00FF00, blue=0x0000FF }; +enum class Productinfo { red=0, purple=1, blue=2 }; + +int webby = blue; // error: blue undefined in this scope +Webcolor webby = Webcolor::blue; + + +void sink(unique_ptr); // consumes the widget + +void sink(widget*); // just uses the widget + +void thinko(const unique_ptr&); // usually not what you want + +void reseat(unique_ptr&); // "will" or "might" reseat pointer + +constexpr int max = 8*1024; +int buf[max]; // OK, but suspicious: uninitialized +f.read(buf, max); + +constexpr int max = 8*1024; +int buf[max] = {0}; // better in some situations +f.read(buf, max); + +string s; // s is default initialized to "" +cin >> s; // s expands to hold the string + + +error_code ec; +Value v = [&] { + auto p = get_value(); // get_value() returns a pair + ec = p.first; + return p.second; +}(); + +Value v = [] { + auto p = get_value(); // get_value() returns a pair + if (p.first) throw Bad_value{p.first}; + return p.second; +}(); + +SomeLargeType var; // ugly CaMeLcAsEvArIaBlE + +if (cond) // some non-trivial condition + Set(&var); +else if (cond2 || !cond3) { + var = Set2(3.14); +} +else { + var = 0; + for (auto& e : something) + var += e; +} + +string var = [&]{ + if (!in) return ""; // default + string s; + for (char c : in >> c) + s += toupper(c); + return s; +}(); // note () + +void use(int n) +{ + switch (n) { // good + case 0: // ... + case 7: // ... + } +} + +int n = numeric_limits::max(); +int m = n + 1; // bad + +std::string s = "hello world"; +double* p = (double*)(&s); // BAD + +class base { public: virtual ~base() = 0; }; + +class derived1 : public base { }; + +class derived2 : public base { + std::string s; +public: + std::string get_s() { return s; } +}; + +derived1 d1; +base* p = &d1; // ok, implicit conversion to pointer to base is fine + +derived2* p2 = (derived2*)(p); // BAD, tries to treat d1 as a derived2, which it is not +cout << p2.get_s(); // tries to access d1's nonexistent string member, instead sees arbitrary bytes near d1 + +void f(const int& i) { + (int&)(i) = 42; // BAD +} + +static int i = 0; +static const int j = 0; + +f(i); // silent side effect +f(j); // undefined behavior + + +auto x = m*v1 + vv; // multiply m with v1 and add the result to vv + +int i; +for (i = 0; i < max; ++i); // bug waiting to happen +if (i == j) + return i;