Skip to content

Commit f9014a6

Browse files
authored
clang-tidy with readability checks (carbon-language#1148)
1 parent 9e7a965 commit f9014a6

18 files changed

+68
-56
lines changed

.clang-tidy

+6-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@ Checks:
1616
-modernize-avoid-c-arrays, -modernize-return-braced-init-list,
1717
-modernize-use-default-member-init, -modernize-use-emplace,
1818
-modernize-use-nodiscard, performance-*, -performance-unnecessary-value-param,
19-
readability-braces-around-statements, readability-identifier-naming
19+
readability-*, -readability-convert-member-functions-to-static,
20+
-readability-function-cognitive-complexity, -readability-else-after-return,
21+
-readability-implicit-bool-conversion, -readability-magic-numbers,
22+
-readability-make-member-function-const,
23+
-readability-qualified-auto, -readability-static-definition-in-anonymous-namespace,
24+
-readability-suspicious-call-argument, -readability-use-anyofallof
2025
WarningsAsErrors: true
2126
CheckOptions:
2227
- { key: readability-identifier-naming.ClassCase, value: CamelCase }

common/check_internal.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,15 @@ class ExitingStream {
5252
return *this;
5353
}
5454

55-
auto operator<<(AddSeparator /*unused*/) -> ExitingStream& {
55+
auto operator<<(AddSeparator /*discarded*/) -> ExitingStream& {
5656
separator_ = true;
5757
return *this;
5858
}
5959

6060
// Low-precedence binary operator overload used in check.h macros to flush the
6161
// output and exit the program. We do this in a binary operator rather than
6262
// the destructor to ensure good debug info and backtraces for errors.
63-
[[noreturn]] friend auto operator|(Helper /*unused*/, ExitingStream& rhs) {
63+
[[noreturn]] friend auto operator|(Helper /*discarded*/, ExitingStream& rhs) {
6464
// Finish with a newline.
6565
llvm::errs() << "\n";
6666
if (rhs.treat_as_bug_) {

executable_semantics/common/error.h

+12-9
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,11 @@ class ErrorBuilder {
3333
return *this;
3434
}
3535

36+
// NOLINTNEXTLINE(google-explicit-constructor): Implicit cast for returns.
3637
operator Error() { return Error(message_); }
3738

3839
template <typename V>
40+
// NOLINTNEXTLINE(google-explicit-constructor): Implicit cast for returns.
3941
operator ErrorOr<V>() {
4042
return Error(message_);
4143
}
@@ -81,21 +83,22 @@ class ErrorBuilder {
8183
#define MAKE_UNIQUE_NAME_IMPL(a, b, c) a##b##c
8284
#define MAKE_UNIQUE_NAME(a, b, c) MAKE_UNIQUE_NAME_IMPL(a, b, c)
8385

84-
#define RETURN_IF_ERROR_IMPL(unique_name, expr) \
85-
if (auto unique_name = (expr); !unique_name.ok()) { \
86-
return std::move(unique_name).error(); \
86+
#define RETURN_IF_ERROR_IMPL(unique_name, expr) \
87+
if (auto unique_name = (expr); /* NOLINT(bugprone-macro-parentheses) */ \
88+
!(unique_name).ok()) { \
89+
return std::move(unique_name).error(); \
8790
}
8891

8992
#define RETURN_IF_ERROR(expr) \
9093
RETURN_IF_ERROR_IMPL( \
9194
MAKE_UNIQUE_NAME(_llvm_error_line, __LINE__, __COUNTER__), expr)
9295

93-
#define ASSIGN_OR_RETURN_IMPL(unique_name, var, expr) \
94-
auto unique_name = (expr); \
95-
if (!unique_name.ok()) { \
96-
return std::move(unique_name).error(); \
97-
} \
98-
var = std::move(*unique_name);
96+
#define ASSIGN_OR_RETURN_IMPL(unique_name, var, expr) \
97+
auto unique_name = (expr); /* NOLINT(bugprone-macro-parentheses) */ \
98+
if (!(unique_name).ok()) { \
99+
return std::move(unique_name).error(); \
100+
} \
101+
var = std::move(*(unique_name)); /* NOLINT(bugprone-macro-parentheses) */
99102

100103
#define ASSIGN_OR_RETURN(var, expr) \
101104
ASSIGN_OR_RETURN_IMPL( \

executable_semantics/common/error_test.cpp

-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
namespace Carbon::Testing {
1010
namespace {
1111

12-
using ::testing::Eq;
13-
1412
auto MakeSuccess() -> ErrorOr<Success> { return Success(); }
1513

1614
auto MakeError(std::string_view message) -> ErrorOr<Success> {

executable_semantics/interpreter/action.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ void Action::Print(llvm::raw_ostream& out) const {
116116
out << "ScopeAction";
117117
}
118118
out << "<" << pos_ << ">";
119-
if (results_.size() > 0) {
119+
if (!results_.empty()) {
120120
out << "(";
121121
llvm::ListSeparator sep;
122122
for (auto& result : results_) {

executable_semantics/interpreter/type_checker.cpp

+21-20
Original file line numberDiff line numberDiff line change
@@ -1183,27 +1183,27 @@ auto TypeChecker::ExpectReturnOnAllPaths(
11831183
// TODO: Add checking to function definitions to ensure that
11841184
// all deduced type parameters will be deduced.
11851185
auto TypeChecker::DeclareFunctionDeclaration(Nonnull<FunctionDeclaration*> f,
1186-
const ImplScope& impl_scope)
1186+
const ImplScope& enclosing_scope)
11871187
-> ErrorOr<Success> {
11881188
if (trace_) {
11891189
llvm::outs() << "** declaring function " << f->name() << "\n";
11901190
}
11911191
// Bring the deduced parameters into scope
11921192
for (Nonnull<GenericBinding*> deduced : f->deduced_parameters()) {
1193-
RETURN_IF_ERROR(TypeCheckExp(&deduced->type(), impl_scope));
1193+
RETURN_IF_ERROR(TypeCheckExp(&deduced->type(), enclosing_scope));
11941194
SetConstantValue(deduced, arena_->New<VariableType>(deduced));
11951195
ASSIGN_OR_RETURN(Nonnull<const Value*> deduced_type,
11961196
InterpExp(&deduced->type(), arena_, trace_));
11971197
deduced->set_static_type(deduced_type);
11981198
}
11991199
// Type check the receiver pattern
12001200
if (f->is_method()) {
1201-
RETURN_IF_ERROR(TypeCheckPattern(&f->me_pattern(), std::nullopt, impl_scope,
1202-
ValueCategory::Let));
1201+
RETURN_IF_ERROR(TypeCheckPattern(&f->me_pattern(), std::nullopt,
1202+
enclosing_scope, ValueCategory::Let));
12031203
}
12041204
// Type check the parameter pattern
12051205
RETURN_IF_ERROR(TypeCheckPattern(&f->param_pattern(), std::nullopt,
1206-
impl_scope, ValueCategory::Let));
1206+
enclosing_scope, ValueCategory::Let));
12071207

12081208
// Create the impl_bindings
12091209
std::vector<Nonnull<const ImplBinding*>> impl_bindings;
@@ -1221,7 +1221,7 @@ auto TypeChecker::DeclareFunctionDeclaration(Nonnull<FunctionDeclaration*> f,
12211221
return_expression.has_value()) {
12221222
// We ignore the return value because return type expressions can't bring
12231223
// new types into scope.
1224-
RETURN_IF_ERROR(TypeCheckExp(*return_expression, impl_scope));
1224+
RETURN_IF_ERROR(TypeCheckExp(*return_expression, enclosing_scope));
12251225
// Should we be doing SetConstantValue instead? -Jeremy
12261226
// And shouldn't the type of this be Type?
12271227
ASSIGN_OR_RETURN(Nonnull<const Value*> ret_type,
@@ -1237,13 +1237,13 @@ auto TypeChecker::DeclareFunctionDeclaration(Nonnull<FunctionDeclaration*> f,
12371237
}
12381238
// Bring the impl bindings into scope
12391239
ImplScope function_scope;
1240-
function_scope.AddParent(&impl_scope);
1240+
function_scope.AddParent(&enclosing_scope);
12411241
for (Nonnull<const ImplBinding*> impl_binding : impl_bindings) {
12421242
function_scope.Add(impl_binding->interface(),
12431243
*impl_binding->type_var()->constant_value(),
12441244
impl_binding);
12451245
}
1246-
RETURN_IF_ERROR(TypeCheckStmt(*f->body(), impl_scope));
1246+
RETURN_IF_ERROR(TypeCheckStmt(*f->body(), enclosing_scope));
12471247
if (!f->return_term().is_omitted()) {
12481248
RETURN_IF_ERROR(ExpectReturnOnAllPaths(f->body(), f->source_loc()));
12491249
}
@@ -1417,11 +1417,11 @@ auto TypeChecker::TypeCheckImplDeclaration(Nonnull<ImplDeclaration*> impl_decl,
14171417
}
14181418

14191419
auto TypeChecker::DeclareChoiceDeclaration(Nonnull<ChoiceDeclaration*> choice,
1420-
const ImplScope& impl_scope)
1420+
const ImplScope& enclosing_scope)
14211421
-> ErrorOr<Success> {
14221422
std::vector<NamedValue> alternatives;
14231423
for (Nonnull<AlternativeSignature*> alternative : choice->alternatives()) {
1424-
RETURN_IF_ERROR(TypeCheckExp(&alternative->signature(), impl_scope));
1424+
RETURN_IF_ERROR(TypeCheckExp(&alternative->signature(), enclosing_scope));
14251425
ASSIGN_OR_RETURN(auto signature,
14261426
InterpExp(&alternative->signature(), arena_, trace_));
14271427
alternatives.push_back({.name = alternative->name(), .value = signature});
@@ -1432,8 +1432,8 @@ auto TypeChecker::DeclareChoiceDeclaration(Nonnull<ChoiceDeclaration*> choice,
14321432
return Success();
14331433
}
14341434

1435-
auto TypeChecker::TypeCheckChoiceDeclaration(Nonnull<ChoiceDeclaration*> choice,
1436-
const ImplScope& impl_scope)
1435+
auto TypeChecker::TypeCheckChoiceDeclaration(
1436+
Nonnull<ChoiceDeclaration*> /*choice*/, const ImplScope& /*impl_scope*/)
14371437
-> ErrorOr<Success> {
14381438
// Nothing to do here, but perhaps that will change in the future?
14391439
return Success();
@@ -1504,34 +1504,35 @@ auto TypeChecker::TypeCheckDeclaration(Nonnull<Declaration*> d,
15041504
}
15051505

15061506
auto TypeChecker::DeclareDeclaration(Nonnull<Declaration*> d,
1507-
ImplScope& impl_scope)
1507+
ImplScope& enclosing_scope)
15081508
-> ErrorOr<Success> {
15091509
switch (d->kind()) {
15101510
case DeclarationKind::InterfaceDeclaration: {
15111511
auto& iface_decl = cast<InterfaceDeclaration>(*d);
1512-
RETURN_IF_ERROR(DeclareInterfaceDeclaration(&iface_decl, impl_scope));
1512+
RETURN_IF_ERROR(
1513+
DeclareInterfaceDeclaration(&iface_decl, enclosing_scope));
15131514
break;
15141515
}
15151516
case DeclarationKind::ImplDeclaration: {
15161517
auto& impl_decl = cast<ImplDeclaration>(*d);
1517-
RETURN_IF_ERROR(DeclareImplDeclaration(&impl_decl, impl_scope));
1518+
RETURN_IF_ERROR(DeclareImplDeclaration(&impl_decl, enclosing_scope));
15181519
break;
15191520
}
15201521
case DeclarationKind::FunctionDeclaration: {
15211522
auto& func_def = cast<FunctionDeclaration>(*d);
1522-
RETURN_IF_ERROR(DeclareFunctionDeclaration(&func_def, impl_scope));
1523+
RETURN_IF_ERROR(DeclareFunctionDeclaration(&func_def, enclosing_scope));
15231524
break;
15241525
}
15251526

15261527
case DeclarationKind::ClassDeclaration: {
15271528
auto& class_decl = cast<ClassDeclaration>(*d);
1528-
RETURN_IF_ERROR(DeclareClassDeclaration(&class_decl, impl_scope));
1529+
RETURN_IF_ERROR(DeclareClassDeclaration(&class_decl, enclosing_scope));
15291530
break;
15301531
}
15311532

15321533
case DeclarationKind::ChoiceDeclaration: {
15331534
auto& choice = cast<ChoiceDeclaration>(*d);
1534-
RETURN_IF_ERROR(DeclareChoiceDeclaration(&choice, impl_scope));
1535+
RETURN_IF_ERROR(DeclareChoiceDeclaration(&choice, enclosing_scope));
15351536
break;
15361537
}
15371538

@@ -1545,8 +1546,8 @@ auto TypeChecker::DeclareDeclaration(Nonnull<Declaration*> d,
15451546
}
15461547
Expression& type =
15471548
cast<ExpressionPattern>(var.binding().type()).expression();
1548-
RETURN_IF_ERROR(TypeCheckPattern(&var.binding(), std::nullopt, impl_scope,
1549-
var.value_category()));
1549+
RETURN_IF_ERROR(TypeCheckPattern(&var.binding(), std::nullopt,
1550+
enclosing_scope, var.value_category()));
15501551
ASSIGN_OR_RETURN(Nonnull<const Value*> declared_type,
15511552
InterpExp(&type, arena_, trace_));
15521553
var.set_static_type(declared_type);

executable_semantics/interpreter/type_checker.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,11 @@ class TypeChecker {
139139
Nonnull<const Value*>>& dict,
140140
Nonnull<const Value*> type) -> Nonnull<const Value*>;
141141

142-
// Sets named_entity.constant_value() to `value`. Can be called multiple
143-
// times on the same named_entity, so long as it is always called with
142+
// Sets value_node.constant_value() to `value`. Can be called multiple
143+
// times on the same value_node, so long as it is always called with
144144
// the same value.
145145
template <typename T>
146-
void SetConstantValue(Nonnull<T*> named_entity, Nonnull<const Value*> value);
146+
void SetConstantValue(Nonnull<T*> value_node, Nonnull<const Value*> value);
147147

148148
void PrintConstants(llvm::raw_ostream& out);
149149

executable_semantics/interpreter/value.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ void Value::Print(llvm::raw_ostream& out) const {
266266
case Value::Kind::FunctionType: {
267267
const auto& fn_type = cast<FunctionType>(*this);
268268
out << "fn ";
269-
if (fn_type.deduced().size() > 0) {
269+
if (!fn_type.deduced().empty()) {
270270
out << "[";
271271
unsigned int i = 0;
272272
for (Nonnull<const GenericBinding*> deduced : fn_type.deduced()) {

executable_semantics/interpreter/value.h

-2
Original file line numberDiff line numberDiff line change
@@ -525,8 +525,6 @@ class Witness : public Value {
525525
Nonnull<const ImplDeclaration*> declaration_;
526526
};
527527

528-
auto FieldTypes(const NominalClassType&) -> std::vector<NamedValue>;
529-
530528
// A choice type.
531529
class ChoiceType : public Value {
532530
public:

toolchain/common/yaml_test_helpers.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ auto operator<<(std::ostream& os, const Value& v) -> std::ostream& {
7272
// Variant visitor that prints the value in the form of code to recreate the
7373
// value.
7474
struct Printer {
75-
auto operator()(NullValue) -> void { out << "Yaml::NullValue()"; }
76-
auto operator()(AliasValue) -> void { out << "Yaml::AliasValue()"; }
77-
auto operator()(ErrorValue) -> void { out << "Yaml::ErrorValue()"; }
75+
auto operator()(NullValue /*v*/) -> void { out << "Yaml::NullValue()"; }
76+
auto operator()(AliasValue /*v*/) -> void { out << "Yaml::AliasValue()"; }
77+
auto operator()(ErrorValue /*v*/) -> void { out << "Yaml::ErrorValue()"; }
7878
auto operator()(const ScalarValue& v) -> void { out << std::quoted(v); }
7979
auto operator()(const MappingValue& v) -> void {
8080
out << "Yaml::MappingValue{";

toolchain/common/yaml_test_helpers.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,12 @@
6060
namespace Carbon::Testing::Yaml {
6161

6262
struct EmptyComparable {
63-
friend auto operator==(EmptyComparable, EmptyComparable) -> bool {
63+
friend auto operator==(EmptyComparable /*lhs*/, EmptyComparable /*rhs*/)
64+
-> bool {
6465
return true;
6566
}
66-
friend auto operator!=(EmptyComparable, EmptyComparable) -> bool {
67+
friend auto operator!=(EmptyComparable /*lhs*/, EmptyComparable /*rhs*/)
68+
-> bool {
6769
return false;
6870
}
6971
};

toolchain/diagnostics/null_diagnostics.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ template <typename LocationT>
1313
inline auto NullDiagnosticLocationTranslator()
1414
-> DiagnosticLocationTranslator<LocationT>& {
1515
struct Translator : DiagnosticLocationTranslator<LocationT> {
16-
auto GetLocation(LocationT) -> Diagnostic::Location override { return {}; }
16+
auto GetLocation(LocationT /*loc*/) -> Diagnostic::Location override {
17+
return {};
18+
}
1719
};
1820
static auto* translator = new Translator;
1921
return *translator;

toolchain/lexer/numeric_literal.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ auto LexedNumericLiteral::Lex(llvm::StringRef source_text)
118118
//
119119
// TODO(zygoloid): Update lexical rules to specify that a numeric literal
120120
// cannot be immediately followed by an alphanumeric character.
121-
int i = 1, n = source_text.size();
121+
int i = 1;
122+
int n = source_text.size();
122123
for (; i != n; ++i) {
123124
char c = source_text[i];
124125
if (IsAlnum(c) || c == '_') {
@@ -305,7 +306,7 @@ auto LexedNumericLiteral::Parser::GetExponent() -> llvm::APInt {
305306
// include a sign bit. Also make sure the exponent isn't too narrow so
306307
// the calculation below can't lose information through overflow.
307308
if (exponent.isSignBitSet() || exponent.getBitWidth() < 64) {
308-
exponent = exponent.zext(std::max(64u, exponent.getBitWidth() + 1));
309+
exponent = exponent.zext(std::max(64U, exponent.getBitWidth() + 1));
309310
}
310311
if (exponent_is_negative_) {
311312
exponent.negate();

toolchain/lexer/tokenized_buffer.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ class TokenizedBuffer::Lexer {
106106
// Consumes (and discard) a valid token to construct a result
107107
// indicating a token has been produced. Relies on implicit conversions.
108108
// NOLINTNEXTLINE(google-explicit-constructor)
109-
LexResult(Token) : LexResult(true) {}
109+
LexResult(Token /*discarded_token*/) : LexResult(true) {}
110110

111111
// Returns a result indicating no token was produced.
112112
static auto NoMatch() -> LexResult { return LexResult(false); }

toolchain/lexer/tokenized_buffer.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ class TokenizedBuffer {
382382

383383
// Map the given position within the source buffer into a diagnostic
384384
// location.
385-
auto GetLocation(const char* pos) -> Diagnostic::Location override;
385+
auto GetLocation(const char* loc) -> Diagnostic::Location override;
386386

387387
private:
388388
TokenizedBuffer* buffer_;
@@ -396,7 +396,7 @@ class TokenizedBuffer {
396396
struct PrintWidths {
397397
// Widens `this` to the maximum of `this` and `new_width` for each
398398
// dimension.
399-
auto Widen(const PrintWidths& new_width) -> void;
399+
auto Widen(const PrintWidths& widths) -> void;
400400

401401
int index;
402402
int kind;

toolchain/lexer/tokenized_buffer_test.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -919,7 +919,7 @@ TEST_F(LexerTest, TypeLiterals) {
919919
auto token_i20 = buffer.tokens().begin() + 2;
920920
EXPECT_EQ(buffer.GetTypeLiteralSize(*token_i20), 20);
921921
auto token_i999999999999 = buffer.tokens().begin() + 3;
922-
EXPECT_EQ(buffer.GetTypeLiteralSize(*token_i999999999999), 999999999999ull);
922+
EXPECT_EQ(buffer.GetTypeLiteralSize(*token_i999999999999), 999999999999ULL);
923923
auto token_u1 = buffer.tokens().begin() + 6;
924924
EXPECT_EQ(buffer.GetTypeLiteralSize(*token_u1), 1);
925925
auto token_u64 = buffer.tokens().begin() + 7;

toolchain/parser/parse_test_helpers.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,10 @@ auto MatchNode(Args... args) -> ExpectedNode {
289289
void UpdateExpectationsForArg(std::string text) {
290290
expected.text = std::move(text);
291291
}
292-
void UpdateExpectationsForArg(HasErrorTag) { expected.has_error = true; }
293-
void UpdateExpectationsForArg(AnyChildrenTag) {
292+
void UpdateExpectationsForArg(HasErrorTag /*tag*/) {
293+
expected.has_error = true;
294+
}
295+
void UpdateExpectationsForArg(AnyChildrenTag /*tag*/) {
294296
expected.skip_subtree = true;
295297
}
296298
void UpdateExpectationsForArg(ExpectedNode node) {

toolchain/parser/parser_impl.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class ParseTree::Parser {
2020
// Parses the tokens into a parse tree, emitting any errors encountered.
2121
//
2222
// This is the entry point to the parser implementation.
23-
static auto Parse(TokenizedBuffer& tokens, TokenDiagnosticEmitter& de)
23+
static auto Parse(TokenizedBuffer& tokens, TokenDiagnosticEmitter& emitter)
2424
-> ParseTree;
2525

2626
private:

0 commit comments

Comments
 (0)