Skip to content

Latest commit

 

History

History
203 lines (118 loc) · 13.1 KB

functions.md

File metadata and controls

203 lines (118 loc) · 13.1 KB
  • See Keep functions short
  • Flag identical and very similar lambdas used in different places.

clang-tidy: readability-function-size
codecompass: similarity checking is implemented there

  • Consider functions with more than one "out" parameter suspicious. Use return values instead, including tuple for multiple return values.
  • Consider "large" functions that don't fit on one editor screen suspicious. Consider factoring such a function into smaller well-named suboperations.
  • Consider functions with 7 or more parameters suspicious.

partial enforcement
clang-tidy: readability-function-size
TODO warn for multiple out parameters

  • Flag functions that do not "fit on a screen." How big is a screen? Try 60 lines by 140 characters; that's roughly the maximum that's comfortable for a book page.
  • Flag functions that are too complex. How complex is too complex? You could use cyclomatic complexity. Try "more than 10 logical path through." Count a simple switch as one path.

clang-tidy: readability-function-size, readability-cognitive-complexity

core-check: C26498 USE_CONSTEXPR_FOR_FUNCTIONCALL

  • Flag inline functions that are more than three statements and could have been declared out of line (such as class member functions).

no enforcement

  • Flag functions that are not noexcept, yet cannot throw.
  • Flag throwing swap, move, destructors, and default constructors.

partial enforcement
clang-tidy: performance-noexcept-move-constructor, there is frontend diagnostic on noexcept dtors
TODO alias into CppCoreGuidelines
TODO throwing swap and default constructors are not enforced

core-check: C26439 SPECIAL_NOEXCEPT, C26440 DECLARE_NOEXCEPT

  • copyable but never copied/moved from or movable but never moved
  • and that is never modified or passed along to another function that could do so.

no enforcement

  • Flag named unused parameters

clang: -Wunused-parameter

  • (Simple) ((Foundation)) Warn when a parameter being passed by value has a size greater than 4 * sizeof(int). Suggest using a reference to const instead.
  • (Simple) ((Foundation)) Warn when a const parameter being passed by reference has a size less than 3 * sizeof(int). Suggest passing by value instead.
  • (Simple) ((Foundation)) Warn when a const parameter being passed by reference is moved.

no enforcement

  • (Moderate) ((Foundation)) Warn about functions regarding reference to non-const parameters that do not write to them.
  • (Simple) ((Foundation)) Warn when a non-const parameter being passed by reference is moved.

no enforcement

  • Flag all X&& parameters (where X is not a template type parameter name) where the function body uses them without std::move.
  • Flag access to moved-from objects.
  • Don't conditionally move from objects

partial enforcement
clang-tidy: bugprone-use-after-move

  • Flag a function that takes a TP&& parameter (where TP is a template type parameter name) and does anything with it other than std::forwarding it exactly once on every static path.

no enforcement
Similar and related: clang-tidy: bugprone-move-forwarding-reference

  • Flag reference to non-const parameters that are not read before being written to and are a type that could be cheaply returned; they should be "out" return values.
  • Flag returning a const value. To fix: Remove const to return a non-const value instead.

no enforcement

  • Output parameters should be replaced by return values. An output parameter is one that the function writes to, invokes a non-const member function, or passes on as a non-const.

no enforcement

  • (Simple) ((Bounds)) Warn for any arithmetic operation on an expression of pointer type that results in a value of pointer type.

clang-tidy: cppgoreguidelines-owning-memory, cppgoreguidelines-pro-bounds-

  • (Simple) Warn if a raw pointer is dereferenced without being tested against nullptr (or equivalent) within a function, suggest it is declared not_null instead.
  • (Simple) Error if a raw pointer is sometimes dereferenced after first being tested against nullptr (or equivalent) within the function and sometimes is not.
  • (Simple) Warn if a not_null pointer is tested against nullptr within a function.

core-check: C26429 USE_NOTNULL, C26430 TEST_ON_ALL_PATHS, C26431 DONT_TEST_NOTNULL

  • (Complex) Warn where accesses to pointer parameters are bounded by other parameters that are integral types and suggest they could use span instead.

no enforcement

  • (Simple) Warn if a function returns a locally allocated raw pointer. Suggest using either unique_ptr or shared_ptr instead.

clang-tidy: cppcoreguidelines-owning-memory

  • Flag delete, std::free(), etc. applied to a plain T*. Only owners should be deleted.
  • Flag new, malloc(), etc. assigned to a plain T*. Only owners should be responsible for deletion.

clang-tidy: cppcoreguidelines-owning-memory, cppcoreguidelines-no-malloc

  • Compilers tend to catch return of reference to locals and could in many cases catch return of pointers to locals.
  • Static analysis can catch many common patterns of the use of pointers indicating positions (thus eliminating dangling pointers)

clang-tidy: clang-analyzer-core.StackAddressEscape

  • Flag functions where no return expression could yield nullptr

no enforcement

  • Flag any use of && as a return type, except in std::move and std::forward.

no enforcement

clang++ required int as return value, otherwise compile error

  • This should be enforced by tooling by checking the return type (and return value) of any assignment operator.

clang-tidy: cppcoreguidelines-c-copy-assignment-signature

  • Warn on use of a named non-generic lambda (e.g., auto x = [](int i){ /*...*/; };) that captures nothing and appears at global scope. Write an ordinary function instead.

no enforcement

  • Flag a lambda that captures by reference, but is used other than locally within the function scope or passed to a function by reference. (Note: This rule is an approximation, but does flag passing by pointer as those are more likely to be stored by the callee, writing to a heap location accessed via a parameter, returning the lambda, etc. The Lifetime rules will also provide general rules that flag escaping pointers and references including via lambdas.)

no enforcement

  • (Simple) Warn when capture-list contains a reference to a locally declared variable
  • (Complex) Flag when capture-list contains a reference to a locally declared variable and the lambda is passed to a non-const and non-local context

no enforcement

  • Flag any lambda capture-list that specifies a default capture and also captures this (whether explicitly or via default capture)

no enforcement

  • Issue a diagnostic for using va_list, va_start, or va_arg.
  • Issue a diagnostic for passing an argument to a vararg parameter of a function that does not offer an overload for a more specific type in the position of the vararg. To fix: Use a different function, or [[suppress(types)]].

clang-tidy: cppcoreguidelines-pro-type-vararg