From 4e1b585fcf640a6d07a5dfb9d8041ceb63c4dad4 Mon Sep 17 00:00:00 2001 From: Jon Ross-Perkins Date: Thu, 2 Feb 2023 17:43:10 -0800 Subject: [PATCH] clang-tidy --fix (#2577) Only automatic fixes. --- common/vlog_internal.h | 2 +- explorer/ast/declaration.h | 2 +- explorer/ast/statement.h | 2 +- explorer/ast/static_scope.h | 2 +- explorer/interpreter/impl_scope.cpp | 4 +-- explorer/interpreter/interpreter.cpp | 10 +++--- explorer/interpreter/type_checker.cpp | 41 ++++++++++++----------- explorer/interpreter/value_transform.h | 4 +-- toolchain/lexer/tokenized_buffer.cpp | 6 ++-- toolchain/parser/parse_tree_test.cpp | 2 -- toolchain/semantics/semantics_ir_test.cpp | 2 -- 11 files changed, 38 insertions(+), 39 deletions(-) diff --git a/common/vlog_internal.h b/common/vlog_internal.h index d296f5c641b7..58ab072b67df 100644 --- a/common/vlog_internal.h +++ b/common/vlog_internal.h @@ -18,7 +18,7 @@ class VLoggingStream { // Internal type used in macros to dispatch to the `operator|` overload. struct Helper {}; - VLoggingStream(llvm::raw_ostream* stream) + explicit VLoggingStream(llvm::raw_ostream* stream) // Prefix the buffer with the current bug report message. : stream_(stream) {} diff --git a/explorer/ast/declaration.h b/explorer/ast/declaration.h index af973d4c74fa..4bf93653867b 100644 --- a/explorer/ast/declaration.h +++ b/explorer/ast/declaration.h @@ -175,7 +175,7 @@ class NamespaceDeclaration : public Declaration { explicit NamespaceDeclaration(SourceLocation source_loc, std::string name) : Declaration(AstNodeKind::NamespaceDeclaration, source_loc), - name_(name) {} + name_(std::move(name)) {} static auto classof(const AstNode* node) -> bool { return InheritsFromNamespaceDeclaration(node->kind()); diff --git a/explorer/ast/statement.h b/explorer/ast/statement.h index eb29efc1bd3f..df653fe75442 100644 --- a/explorer/ast/statement.h +++ b/explorer/ast/statement.h @@ -163,7 +163,7 @@ class IncrementDecrement : public Statement { auto argument() const -> const Expression& { return *argument_; } auto argument() -> Expression& { return *argument_; } - bool is_increment() const { return is_increment_; } + auto is_increment() const -> bool { return is_increment_; } // Set the rewritten form of this statement. Can only be called during type // checking. diff --git a/explorer/ast/static_scope.h b/explorer/ast/static_scope.h index d3b89cb0002a..a5065db5574a 100644 --- a/explorer/ast/static_scope.h +++ b/explorer/ast/static_scope.h @@ -35,7 +35,7 @@ class StaticScope { }; // Construct a root scope. - StaticScope() {} + StaticScope() = default; // Construct a scope that is nested within the given scope. explicit StaticScope(Nonnull parent) diff --git a/explorer/interpreter/impl_scope.cpp b/explorer/interpreter/impl_scope.cpp index 2ed0ad6c1256..c3f8c6b114a4 100644 --- a/explorer/interpreter/impl_scope.cpp +++ b/explorer/interpreter/impl_scope.cpp @@ -171,7 +171,7 @@ auto ImplScope::Resolve(Nonnull constraint_type, current, &equality_ctx)); } } - for (auto& rewrite : rewrites) { + for (const auto& rewrite : rewrites) { Nonnull constant = type_checker.Substitute(local_bindings, rewrite.constant); Nonnull value = type_checker.Substitute( @@ -260,7 +260,7 @@ static auto CombineResults(Nonnull iface_type, if (impl_a->declaration().match_first() && impl_a->declaration().match_first() == impl_b->declaration().match_first()) { - for (auto* impl : (*impl_a->declaration().match_first())->impls()) { + for (const auto* impl : (*impl_a->declaration().match_first())->impls()) { if (impl == &impl_a->declaration()) { return a; } diff --git a/explorer/interpreter/interpreter.cpp b/explorer/interpreter/interpreter.cpp index 66e23a163b87..33909d7b5744 100644 --- a/explorer/interpreter/interpreter.cpp +++ b/explorer/interpreter/interpreter.cpp @@ -576,7 +576,7 @@ auto Interpreter::EvalAssociatedConstant( Nonnull constraint = impl_witness->declaration().constraint_type(); std::optional> result; - for (auto& rewrite : constraint->rewrite_constraints()) { + for (const auto& rewrite : constraint->rewrite_constraints()) { if (&rewrite.constant->constant() == &assoc->constant() && TypeEqual(&rewrite.constant->interface(), interface, std::nullopt)) { // TODO: The value might depend on the parameters of the impl. We need to @@ -869,7 +869,7 @@ auto Interpreter::Convert(Nonnull value, CARBON_ASSIGN_OR_RETURN( Nonnull value, EvalAssociatedConstant(cast(value), source_loc)); - if (auto* new_const = dyn_cast(value)) { + if (const auto* new_const = dyn_cast(value)) { // TODO: Detect whether conversions are required in type-checking. if (isa( destination_type) && @@ -933,7 +933,7 @@ auto Interpreter::CallDestructor(Nonnull fun, BindingMap generic_args; // TODO: move this logic into PatternMatch, and call it here. - auto p = &method.self_pattern().value(); + const auto* p = &method.self_pattern().value(); const auto& placeholder = cast(*p); if (placeholder.value_node().has_value()) { method_scope.Bind(*placeholder.value_node(), receiver); @@ -1015,7 +1015,7 @@ auto Interpreter::CallFunction(const CallExpression& call, RuntimeScope method_scope(&heap_); BindingMap generic_args; // Bind the receiver to the `self` parameter. - auto p = &method.self_pattern().value(); + const auto* p = &method.self_pattern().value(); if (p->kind() == Value::Kind::BindingPlaceholderValue) { // TODO: move this logic into PatternMatch const auto& placeholder = cast(*p); @@ -1627,7 +1627,7 @@ auto Interpreter::StepExp() -> ErrorOr { case ExpressionKind::ArrayTypeLiteral: case ExpressionKind::ValueLiteral: { CARBON_CHECK(act.pos() == 0); - auto* value = &cast(exp).constant_value(); + const auto* value = &cast(exp).constant_value(); CARBON_ASSIGN_OR_RETURN( Nonnull destination, InstantiateType(&exp.static_type(), exp.source_loc())); diff --git a/explorer/interpreter/type_checker.cpp b/explorer/interpreter/type_checker.cpp index fbc9793e6ccd..4c9cb4ff6d0e 100644 --- a/explorer/interpreter/type_checker.cpp +++ b/explorer/interpreter/type_checker.cpp @@ -817,20 +817,21 @@ auto TypeChecker::ExpectNonPlaceholderType(SourceLocation source_loc, if (!IsPlaceholderType(type)) { return Success(); } - if (auto* member_name = dyn_cast(type)) { + if (const auto* member_name = dyn_cast(type)) { return ProgramError(source_loc) << *member_name << " can only be used in a member access or alias"; } - if (auto* param_entity = dyn_cast(type)) { + if (const auto* param_entity = + dyn_cast(type)) { return ProgramError(source_loc) << "'" << param_entity->name() << "' must be given an argument list"; } - if (auto* mixin_type = dyn_cast(type)) { + if (const auto* mixin_type = dyn_cast(type)) { return ProgramError(source_loc) << "invalid use of mixin " << mixin_type->mixin_type().declaration().name(); } - if (auto* namespace_type = dyn_cast(type)) { + if (const auto* namespace_type = dyn_cast(type)) { return ProgramError(source_loc) << "expected `.member_name` after name of " << *namespace_type; } @@ -1320,7 +1321,7 @@ static auto LookupRewrite(llvm::ArrayRef rewrites, return std::nullopt; } - for (auto& rewrite : rewrites) { + for (const auto& rewrite : rewrites) { if (ValueEqual(interface, &rewrite.constant->interface(), std::nullopt) && member == &rewrite.constant->constant()) { // A ConstraintType can only have one rewrite per (interface, member) @@ -1596,7 +1597,7 @@ class TypeChecker::ConstraintTypeBuilder { // Add all of the new constraints. impl_scope->Add(new_impl_constraints, std::nullopt, std::nullopt, GetSelfWitness(), type_checker); - for (auto& equal : new_equality_constraints) { + for (const auto& equal : new_equality_constraints) { impl_scope->AddEqualityConstraint(arena_->New(equal)); } } @@ -1670,7 +1671,7 @@ class TypeChecker::ConstraintTypeBuilder { if (auto existing_rewrite = LookupRewrite( new_rewrite_constraints, &rewrite_a.constant->interface(), &rewrite_a.constant->constant())) { - auto& rewrite_b = **existing_rewrite; + const auto& rewrite_b = **existing_rewrite; if (ValueEqual(rewrite_a.unconverted_replacement, rewrite_b.unconverted_replacement, std::nullopt) && TypeEqual(rewrite_a.unconverted_replacement_type, @@ -1886,7 +1887,7 @@ auto TypeChecker::Substitute(const Bindings& bindings, return type; } - auto* result = SubstituteImpl(bindings, type); + const auto* result = SubstituteImpl(bindings, type); if (trace_stream_) { **trace_stream_ << "substitution of {"; @@ -1923,7 +1924,7 @@ class TypeChecker::SubstituteTransform -> Nonnull { auto it = bindings_.args().find(&var_type->binding()); if (it == bindings_.args().end()) { - if (auto& trace_stream = type_checker_->trace_stream_) { + if (const auto& trace_stream = type_checker_->trace_stream_) { **trace_stream << "substitution: no value for binding " << *var_type << ", leaving alone\n"; } @@ -1938,7 +1939,7 @@ class TypeChecker::SubstituteTransform -> Nonnull { auto it = bindings_.witnesses().find(witness->binding()); if (it == bindings_.witnesses().end()) { - if (auto& trace_stream = type_checker_->trace_stream_) { + if (const auto& trace_stream = type_checker_->trace_stream_) { **trace_stream << "substitution: no value for binding " << *witness << ", leaving alone\n"; } @@ -2019,7 +2020,7 @@ class TypeChecker::SubstituteTransform } else { type_of_type = type_checker_->arena_->New(); } - if (auto& trace_stream = type_checker_->trace_stream_) { + if (const auto& trace_stream = type_checker_->trace_stream_) { **trace_stream << "substitution: self of constraint " << *constraint << " is substituted, new type of type is " << *type_of_type << "\n"; @@ -2034,7 +2035,7 @@ class TypeChecker::SubstituteTransform builder.GetSelfWitness(), bindings_, /*add_lookup_contexts=*/true); Nonnull new_constraint = std::move(builder).Build(); - if (auto& trace_stream = type_checker_->trace_stream_) { + if (const auto& trace_stream = type_checker_->trace_stream_) { **trace_stream << "substitution: " << *constraint << " => " << *new_constraint << "\n"; } @@ -2063,7 +2064,7 @@ auto TypeChecker::RefineWitness(Nonnull witness, // See if this is already resolved as some number of layers of // ConstraintImplWitness applied to an ImplWitness. Nonnull inner_witness = witness; - while (auto* inner_constraint_impl_witness = + while (const auto* inner_constraint_impl_witness = dyn_cast(inner_witness)) { inner_witness = inner_constraint_impl_witness->constraint_witness(); } @@ -2366,7 +2367,7 @@ auto TypeChecker::LookupRewriteInTypeOf( // interface, member); // where we substitute as little as possible to try to avoid infinite // recursion. - if (auto* constraint = + if (const auto* constraint = dyn_cast(&assoc_const->constant().static_type())) { for (auto rewrite : constraint->rewrite_constraints()) { if (&rewrite.constant->constant() != &assoc_const->constant()) { @@ -2425,7 +2426,7 @@ static auto IsInstanceMember(Nonnull element) { case ElementKind::PositionalElement: return true; case ElementKind::NamedElement: - const auto nom_element = cast(element); + const auto* const nom_element = cast(element); if (!nom_element->declaration()) { // This is a struct field. return true; @@ -2671,7 +2672,8 @@ auto TypeChecker::TypeCheckExp(Nonnull e, access.set_is_type_access(!IsInstanceMember(&access.member())); access.set_static_type(inst_member_type); - if (auto* func_decl = dyn_cast(result.member)) { + if (const auto* func_decl = + dyn_cast(result.member)) { CARBON_RETURN_IF_ERROR( CheckAddrMeAccess(&access, func_decl, bindings, impl_scope)); } @@ -4106,8 +4108,8 @@ auto TypeChecker::TypeCheckGenericBinding(GenericBinding& binding, // Get the builtin interface that should be used for the given kind of // assignment operator. -static Builtins::Builtin GetBuiltinInterfaceForAssignOperator( - AssignOperator op) { +static auto GetBuiltinInterfaceForAssignOperator(AssignOperator op) + -> Builtins::Builtin { switch (op) { case AssignOperator::Plain: return Builtins::AssignWith; @@ -5051,7 +5053,8 @@ auto TypeChecker::DeclareConstraintTypeDeclaration( // constraint for the constraint type: `let X:! Interface` adds a // `Self.X is Interface` constraint that `impl`s must satisfy and users // of the constraint type can rely on. - if (auto* constraint_type = dyn_cast(constraint)) { + if (const auto* constraint_type = + dyn_cast(constraint)) { builder.AddAndSubstitute(*this, constraint_type, assoc_value, builder.GetSelfWitness(), Bindings(), /*add_lookup_contexts=*/false); diff --git a/explorer/interpreter/value_transform.h b/explorer/interpreter/value_transform.h index 679b3776635a..d6f511483971 100644 --- a/explorer/interpreter/value_transform.h +++ b/explorer/interpreter/value_transform.h @@ -37,7 +37,7 @@ constexpr bool IsRecursivelyTransformable< template class TransformBase { public: - TransformBase(Nonnull arena) : arena_(arena) {} + explicit TransformBase(Nonnull arena) : arena_(arena) {} template auto Transform(T&& v) -> decltype(auto) { @@ -134,7 +134,7 @@ class ValueTransform : public TransformBase { // T*)` in the base class. We can remove this once we start using a compiler // that implements P1787R6. template - auto operator()(Nonnull node, int = 0) + auto operator()(Nonnull node, int /*unused*/ = 0) -> std::enable_if_t, Nonnull> { return node; diff --git a/toolchain/lexer/tokenized_buffer.cpp b/toolchain/lexer/tokenized_buffer.cpp index b9f2c8578e49..41ff40f8269c 100644 --- a/toolchain/lexer/tokenized_buffer.cpp +++ b/toolchain/lexer/tokenized_buffer.cpp @@ -895,7 +895,7 @@ auto TokenizedBuffer::SourceBufferLocationTranslator::GetLocation( // Find the first line starting after the given location. Note that we can't // inspect `line.length` here because it is not necessarily correct for the // final line during lexing (but will be correct later for the parse tree). - auto* line_it = std::partition_point( + const auto* line_it = std::partition_point( buffer_->line_infos_.begin(), buffer_->line_infos_.end(), [offset](const LineInfo& line) { return line.start <= offset; }); bool incomplete_line_info = last_line_lexed_to_column_ != nullptr && @@ -932,8 +932,8 @@ auto TokenizedBuffer::SourceBufferLocationTranslator::GetLocation( auto TokenizedBuffer::TokenLocationTranslator::GetLocation(Token token) -> DiagnosticLocation { // Map the token location into a position within the source buffer. - auto& token_info = buffer_->GetTokenInfo(token); - auto& line_info = buffer_->GetLineInfo(token_info.token_line); + const auto& token_info = buffer_->GetTokenInfo(token); + const auto& line_info = buffer_->GetLineInfo(token_info.token_line); const char* token_start = buffer_->source_->text().begin() + line_info.start + token_info.column; diff --git a/toolchain/parser/parse_tree_test.cpp b/toolchain/parser/parse_tree_test.cpp index 9ea023660755..84d61ccaeab9 100644 --- a/toolchain/parser/parse_tree_test.cpp +++ b/toolchain/parser/parse_tree_test.cpp @@ -18,9 +18,7 @@ namespace Carbon::Testing { namespace { -using ::testing::AtLeast; using ::testing::ElementsAre; -using ::testing::Eq; class ParseTreeTest : public ::testing::Test { protected: diff --git a/toolchain/semantics/semantics_ir_test.cpp b/toolchain/semantics/semantics_ir_test.cpp index a5c41f7338d7..acde13b20670 100644 --- a/toolchain/semantics/semantics_ir_test.cpp +++ b/toolchain/semantics/semantics_ir_test.cpp @@ -9,8 +9,6 @@ #include -#include "gmock/gmock.h" -#include "gtest/gtest.h" #include "toolchain/common/yaml_test_helpers.h" #include "toolchain/diagnostics/diagnostic_emitter.h" #include "toolchain/lexer/tokenized_buffer.h"