Skip to content

Commit

Permalink
use abseil's contains method rather than std's find()/end() idiom (#4193
Browse files Browse the repository at this point in the history
)

* convert find() == end() calls to use !contains

* convert find() != end() calls to use contains

* format autoloader.cc
  • Loading branch information
froydnj committed May 3, 2021
1 parent 4e4baac commit e879df6
Show file tree
Hide file tree
Showing 9 changed files with 15 additions and 16 deletions.
4 changes: 2 additions & 2 deletions ast/desugar/Desugar.cc
Expand Up @@ -566,9 +566,9 @@ class DuplicateHashKeyCheck {
return;
}

if (isSymbol && hashKeySymbols.find(nameRef) == hashKeySymbols.end()) {
if (isSymbol && !hashKeySymbols.contains(nameRef)) {
hashKeySymbols[nameRef] = key.loc();
} else if (!isSymbol && hashKeyStrings.find(nameRef) == hashKeyStrings.end()) {
} else if (!isSymbol && !hashKeyStrings.contains(nameRef)) {
hashKeyStrings[nameRef] = key.loc();
} else {
if (auto e = dctx.ctx.beginError(key.loc(), core::errors::Desugar::DuplicatedHashKeys)) {
Expand Down
2 changes: 1 addition & 1 deletion common/common.cc
Expand Up @@ -279,7 +279,7 @@ void appendFilesInDir(string_view basePath, string_view path, const sorbet::Unor
// Note: Can't call substr with an index > string length, so explicitly check if a dot isn't found.
if (dotLocation != string::npos) {
auto ext = fullPath.substr(dotLocation);
if (extensions.find(ext) != extensions.end()) {
if (extensions.contains(ext)) {
result.emplace_back(fullPath);
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/types/calls.cc
Expand Up @@ -972,7 +972,7 @@ DispatchResult dispatchCallSymbol(const GlobalState &gs, const DispatchArgs &arg
}

NameRef arg = key.asName(gs);
if (consumed.find(arg) != consumed.end()) {
if (consumed.contains(arg)) {
continue;
}
consumed.insert(arg);
Expand Down
4 changes: 2 additions & 2 deletions infer/environment.cc
Expand Up @@ -726,7 +726,7 @@ void Environment::assumeKnowledge(core::Context ctx, bool isTrue, cfg::LocalRef
auto &noTests = knowledgeToChoose->noTypeTests;

for (auto &typeTested : yesTests) {
if (filter.find(typeTested.first) == filter.end()) {
if (!filter.contains(typeTested.first)) {
continue;
}
core::TypeAndOrigins tp = getTypeAndOrigin(ctx, typeTested.first);
Expand All @@ -743,7 +743,7 @@ void Environment::assumeKnowledge(core::Context ctx, bool isTrue, cfg::LocalRef
}

for (auto &typeTested : noTests) {
if (filter.find(typeTested.first) == filter.end()) {
if (!filter.contains(typeTested.first)) {
continue;
}
core::TypeAndOrigins tp = getTypeAndOrigin(ctx, typeTested.first);
Expand Down
7 changes: 3 additions & 4 deletions main/autogen/autoloader.cc
Expand Up @@ -19,8 +19,7 @@ namespace sorbet::autogen {
// converts it to a `DefTree`, and then emits the autoloader files based on passed-in string fragments

bool AutoloaderConfig::include(const NamedDefinition &nd) const {
return !nd.qname.nameParts.empty() &&
topLevelNamespaceRefs.find(nd.qname.nameParts[0]) != topLevelNamespaceRefs.end();
return !nd.qname.nameParts.empty() && topLevelNamespaceRefs.contains(nd.qname.nameParts[0]);
}

bool AutoloaderConfig::includePath(string_view path) const {
Expand All @@ -30,11 +29,11 @@ bool AutoloaderConfig::includePath(string_view path) const {
}

bool AutoloaderConfig::includeRequire(core::NameRef req) const {
return excludedRequireRefs.find(req) == excludedRequireRefs.end();
return !excludedRequireRefs.contains(req);
}

bool AutoloaderConfig::sameFileCollapsable(const vector<core::NameRef> &module) const {
return nonCollapsableModuleNames.find(module) == nonCollapsableModuleNames.end();
return !nonCollapsableModuleNames.contains(module);
}

string_view AutoloaderConfig::normalizePath(const core::GlobalState &gs, core::FileRef file) const {
Expand Down
2 changes: 1 addition & 1 deletion main/lsp/requests/completion.cc
Expand Up @@ -156,7 +156,7 @@ SimilarMethodsByName mergeSimilarMethods(SimilarMethodsByName left, SimilarMetho
auto result = SimilarMethodsByName{};

for (auto [methodName, leftSimilarMethods] : left) {
if (right.find(methodName) != right.end()) {
if (right.contains(methodName)) {
for (auto similarMethod : leftSimilarMethods) {
result[methodName].emplace_back(similarMethod);
}
Expand Down
6 changes: 3 additions & 3 deletions main/lsp/tools/generate_lsp_messages.h
Expand Up @@ -823,7 +823,7 @@ class JSONVariantType : public JSONType {
std::vector<std::string> emitOrder;
for (auto &variant : variants) {
auto cppType = variant->getCPPType();
if (uniqueTypes.find(cppType) == uniqueTypes.end()) {
if (!uniqueTypes.contains(cppType)) {
uniqueTypes.insert(cppType);
emitOrder.push_back(cppType);
}
Expand Down Expand Up @@ -930,12 +930,12 @@ class JSONBasicVariantType final : public JSONVariantType {
variant->getJSONBaseKind() == BaseKind::ComplexKind) {
throw std::invalid_argument("Invalid variant type: Complex are not supported.");
}
if (cppKindSeen.find(variant->getCPPBaseKind()) != cppKindSeen.end()) {
if (cppKindSeen.contains(variant->getCPPBaseKind())) {
throw std::invalid_argument(
"Invalid variant type: Cannot discriminate between multiple types with same base C++ kind.");
}
cppKindSeen.insert(variant->getCPPBaseKind());
if (jsonKindSeen.find(variant->getJSONBaseKind()) != jsonKindSeen.end()) {
if (jsonKindSeen.contains(variant->getJSONBaseKind())) {
throw std::invalid_argument(
"Invalid variant type: Cannot discriminate between multiple types with same base JSON kind.");
}
Expand Down
2 changes: 1 addition & 1 deletion test/helpers/position_assertions.cc
Expand Up @@ -360,7 +360,7 @@ vector<shared_ptr<RangeAssertion>> parseAssertionsForFile(const shared_ptr<core:
if (findConstructor != assertionConstructors.end()) {
assertions.push_back(
findConstructor->second(filename, range, lineNum, assertionContents, assertionType));
} else if (ignoredAssertionLabels.find(assertionType) == ignoredAssertionLabels.end()) {
} else if (!ignoredAssertionLabels.contains(assertionType)) {
ADD_FAIL_CHECK_AT(
filename.c_str(), lineNum + 1,
fmt::format("Found unrecognized assertion of type `{}`. Expected one of {{{}}}.\nIf this is a "
Expand Down
2 changes: 1 addition & 1 deletion test/lsp_test_runner.cc
Expand Up @@ -329,7 +329,7 @@ TEST_CASE("LSPTest") {
lspWrapper->enableAllExperimentalFeatures();
}

if (test.expectations.find("autogen") != test.expectations.end()) {
if (test.expectations.contains("autogen")) {
// When autogen is enabled, skip Rewriter passes...
lspWrapper->opts->skipRewriterPasses = true;
// Some autogen tests assume that some errors will occur from the resolver step, others assume the resolver
Expand Down

0 comments on commit e879df6

Please sign in to comment.