From ee4f2492ea4e7ff120f68a792af870ee30435aa5 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Fri, 16 Aug 2019 23:23:07 +0100 Subject: [PATCH] Fixed some lint errors seen in google3. (#6520) --- .../compiler/csharp/csharp_helpers.cc | 2 +- .../csharp/csharp_reflection_class.cc | 3 ++ .../protobuf/compiler/php/php_generator.cc | 51 +++++++++---------- 3 files changed, 27 insertions(+), 29 deletions(-) diff --git a/src/google/protobuf/compiler/csharp/csharp_helpers.cc b/src/google/protobuf/compiler/csharp/csharp_helpers.cc index f0684a6f7f739..50c2c9766d89a 100644 --- a/src/google/protobuf/compiler/csharp/csharp_helpers.cc +++ b/src/google/protobuf/compiler/csharp/csharp_helpers.cc @@ -297,7 +297,7 @@ uint GetGroupEndTag(const Descriptor* descriptor) { std::string ToCSharpName(const std::string& name, const FileDescriptor* file) { std::string result = GetFileNamespace(file); - if (result != "") { + if (!result.empty()) { result += '.'; } string classname; diff --git a/src/google/protobuf/compiler/csharp/csharp_reflection_class.cc b/src/google/protobuf/compiler/csharp/csharp_reflection_class.cc index c481f1400c035..ec816a7584f66 100644 --- a/src/google/protobuf/compiler/csharp/csharp_reflection_class.cc +++ b/src/google/protobuf/compiler/csharp/csharp_reflection_class.cc @@ -261,6 +261,7 @@ void ReflectionClassGenerator::WriteGeneratedCodeInfo(const Descriptor* descript // Fields if (descriptor->field_count() > 0) { std::vector fields; + fields.reserve(descriptor->field_count()); for (int i = 0; i < descriptor->field_count(); i++) { fields.push_back(GetPropertyName(descriptor->field(i))); } @@ -273,6 +274,7 @@ void ReflectionClassGenerator::WriteGeneratedCodeInfo(const Descriptor* descript // Oneofs if (descriptor->oneof_decl_count() > 0) { std::vector oneofs; + oneofs.reserve(descriptor->oneof_decl_count()); for (int i = 0; i < descriptor->oneof_decl_count(); i++) { oneofs.push_back(UnderscoresToCamelCase(descriptor->oneof_decl(i)->name(), true)); } @@ -285,6 +287,7 @@ void ReflectionClassGenerator::WriteGeneratedCodeInfo(const Descriptor* descript // Nested enums if (descriptor->enum_type_count() > 0) { std::vector enums; + enums.reserve(descriptor->enum_type_count()); for (int i = 0; i < descriptor->enum_type_count(); i++) { enums.push_back(GetClassName(descriptor->enum_type(i))); } diff --git a/src/google/protobuf/compiler/php/php_generator.cc b/src/google/protobuf/compiler/php/php_generator.cc index 4a1403f94fc40..29a5ac26423d7 100644 --- a/src/google/protobuf/compiler/php/php_generator.cc +++ b/src/google/protobuf/compiler/php/php_generator.cc @@ -88,7 +88,6 @@ std::string GeneratedMetadataFileName(const FileDescriptor* file, std::string LabelForField(FieldDescriptor* field); std::string TypeName(FieldDescriptor* field); std::string UnderscoresToCamelCase(const string& name, bool cap_first_letter); -std::string EscapeDollor(const string& to_escape); std::string BinaryToHex(const string& binary); void Indent(io::Printer* printer); void Outdent(io::Printer* printer); @@ -153,7 +152,7 @@ template std::string ClassNamePrefix(const string& classname, const DescriptorType* desc) { const string& prefix = (desc->file()->options()).php_class_prefix(); - if (prefix != "") { + if (!prefix.empty()) { return prefix; } @@ -244,13 +243,13 @@ template std::string RootPhpNamespace(const DescriptorType* desc, bool is_descriptor) { if (desc->file()->options().has_php_namespace()) { const string& php_namespace = desc->file()->options().php_namespace(); - if (php_namespace != "") { + if (!php_namespace.empty()) { return php_namespace; } return ""; } - if (desc->file()->package() != "") { + if (!desc->file()->package().empty()) { return PhpName(desc->file()->package(), is_descriptor); } return ""; @@ -260,7 +259,7 @@ template std::string FullClassName(const DescriptorType* desc, bool is_descriptor) { string classname = GeneratedClassNameImpl(desc); string php_namespace = RootPhpNamespace(desc, is_descriptor); - if (php_namespace != "") { + if (!php_namespace.empty()) { return php_namespace + "\\" + classname; } return classname; @@ -270,7 +269,7 @@ template std::string LegacyFullClassName(const DescriptorType* desc, bool is_descriptor) { string classname = LegacyGeneratedClassName(desc); string php_namespace = RootPhpNamespace(desc, is_descriptor); - if (php_namespace != "") { + if (!php_namespace.empty()) { return php_namespace + "\\" + classname; } return classname; @@ -352,7 +351,7 @@ std::string GeneratedMetadataFileName(const FileDescriptor* file, if (file->options().has_php_metadata_namespace()) { const string& php_metadata_namespace = file->options().php_metadata_namespace(); - if (php_metadata_namespace != "" && php_metadata_namespace != "\\") { + if (!php_metadata_namespace.empty() && php_metadata_namespace != "\\") { result += php_metadata_namespace; std::replace(result.begin(), result.end(), '\\', '/'); if (result.at(result.size() - 1) != '/') { @@ -552,45 +551,41 @@ std::string EnumOrMessageSuffix( // Converts a name to camel-case. If cap_first_letter is true, capitalize the // first letter. -std::string UnderscoresToCamelCase(const string& input, bool cap_first_letter) { +std::string UnderscoresToCamelCase(const string& name, bool cap_first_letter) { std::string result; - for (int i = 0; i < input.size(); i++) { - if ('a' <= input[i] && input[i] <= 'z') { + for (int i = 0; i < name.size(); i++) { + if ('a' <= name[i] && name[i] <= 'z') { if (cap_first_letter) { - result += input[i] + ('A' - 'a'); + result += name[i] + ('A' - 'a'); } else { - result += input[i]; + result += name[i]; } cap_first_letter = false; - } else if ('A' <= input[i] && input[i] <= 'Z') { + } else if ('A' <= name[i] && name[i] <= 'Z') { if (i == 0 && !cap_first_letter) { // Force first letter to lower-case unless explicitly told to // capitalize it. - result += input[i] + ('a' - 'A'); + result += name[i] + ('a' - 'A'); } else { // Capital letters after the first are left as-is. - result += input[i]; + result += name[i]; } cap_first_letter = false; - } else if ('0' <= input[i] && input[i] <= '9') { - result += input[i]; + } else if ('0' <= name[i] && name[i] <= '9') { + result += name[i]; cap_first_letter = true; } else { cap_first_letter = true; } } // Add a trailing "_" if the name should be altered. - if (input[input.size() - 1] == '#') { + if (name[name.size() - 1] == '#') { result += '_'; } return result; } -std::string EscapeDollor(const string& to_escape) { - return StringReplace(to_escape, "$", "\\$", true); -} - -std::string BinaryToHex(const string& src) { +std::string BinaryToHex(const string& binary) { string dest; size_t i; unsigned char symbol[16] = { @@ -600,12 +595,12 @@ std::string BinaryToHex(const string& src) { 'c', 'd', 'e', 'f', }; - dest.resize(src.size() * 2); + dest.resize(binary.size() * 2); char* append_ptr = &dest[0]; - for (i = 0; i < src.size(); i++) { - *append_ptr++ = symbol[(src[i] & 0xf0) >> 4]; - *append_ptr++ = symbol[src[i] & 0x0f]; + for (i = 0; i < binary.size(); i++) { + *append_ptr++ = symbol[(binary[i] & 0xf0) >> 4]; + *append_ptr++ = symbol[binary[i] & 0x0f]; } return dest; @@ -1103,7 +1098,7 @@ void LegacyGenerateClassFile(const FileDescriptor* file, const DescriptorType* d GenerateHead(file, &printer); std::string php_namespace = RootPhpNamespace(desc, is_descriptor); - if (php_namespace != "") { + if (!php_namespace.empty()) { printer.Print( "namespace ^name^;\n\n", "name", php_namespace);