Skip to content

[gardening] Use empty() over size() == 0 #14977

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/swift/Basic/Diff.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ class diff_match_patch {
Patch() : start1(0), start2(0), length1(0), length2(0) {}

bool isNull() const {
return start1 == 0 && start2 == 0 && length1 == 0 && length2 == 0 && diffs.size() == 0;
return start1 == 0 && start2 == 0 && length1 == 0 && length2 == 0 && diffs.empty();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion include/swift/SILOptimizer/Analysis/BottomUpIPAnalysis.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ class BottomUpIPAnalysis : public SILAnalysis {
}

~BottomUpFunctionOrder() {
assert(InitiallyUnscheduled.size() == 0 &&
assert(InitiallyUnscheduled.empty() &&
"not finished scheduling");
assert(Scheduled.size() == numVisited &&
"missed some functions to schedule");
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4289,7 +4289,7 @@ void DeclName::CompoundDeclName::Profile(llvm::FoldingSetNodeID &id,

void DeclName::initialize(ASTContext &C, DeclBaseName baseName,
ArrayRef<Identifier> argumentNames) {
if (argumentNames.size() == 0) {
if (argumentNames.empty()) {
SimpleOrCompound = BaseNameAndCompound(baseName, true);
return;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/Builtins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ static ValueDecl *getBinaryPredicate(Identifier Id, Type ArgType) {
static ValueDecl *getCastOperation(ASTContext &Context, Identifier Id,
BuiltinValueKind VK,
ArrayRef<Type> Types) {
if (Types.size() == 0 || Types.size() > 2) return nullptr;
if (Types.empty() || Types.size() > 2) return nullptr;
Type Input = Types[0];
Type Output = Types.size() == 2 ? Types[1] : Type();

Expand Down
6 changes: 3 additions & 3 deletions lib/AST/DocComment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ bool extractParameterOutline(
L->setChildren(NormalItems);
}

return NormalItems.size() == 0;
return NormalItems.empty();
}

bool extractSeparatedParams(
Expand Down Expand Up @@ -205,7 +205,7 @@ bool extractSeparatedParams(
if (NormalItems.size() != Children.size())
L->setChildren(NormalItems);

return NormalItems.size() == 0;
return NormalItems.empty();
}

bool extractSimpleField(
Expand Down Expand Up @@ -280,7 +280,7 @@ bool extractSimpleField(
if (NormalItems.size() != Children.size())
L->setChildren(NormalItems);

return NormalItems.size() == 0;
return NormalItems.empty();
}

swift::markup::CommentParts
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/Expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1207,7 +1207,7 @@ packSingleArgument(ASTContext &ctx, SourceLoc lParenLoc, ArrayRef<Expr *> args,

// If we have no other arguments, represent the trailing closure as a
// parenthesized expression.
if (args.size() == 0) {
if (args.empty()) {
auto arg = new (ctx) ParenExpr(lParenLoc, trailingClosure, rParenLoc,
/*hasTrailingClosure=*/true);
computeSingleArgumentType(ctx, arg, implicit, getType);
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/ProtocolConformance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ ProtocolConformanceRef::getConditionalRequirements() const {
}

void NormalProtocolConformance::differenceAndStoreConditionalRequirements() {
assert(ConditionalRequirements.size() == 0 &&
assert(ConditionalRequirements.empty() &&
"should not recompute conditional requirements");
auto &ctxt = getProtocol()->getASTContext();
auto DC = getDeclContext();
Expand Down
2 changes: 1 addition & 1 deletion lib/Basic/Version.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ Optional<Version> Version::parseCompilerVersionString(
bool isValidVersion = true;

auto checkVersionComponent = [&](unsigned Component, SourceRange Range) {
unsigned limit = CV.Components.size() == 0 ? 9223371 : 999;
unsigned limit = CV.Components.empty() ? 9223371 : 999;

if (Component > limit) {
if (Diags)
Expand Down
2 changes: 1 addition & 1 deletion lib/ClangImporter/ImportType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2191,7 +2191,7 @@ ImportedType ClangImporter::Implementation::importAccessorMethodType(

// Determine if the method is a property getter or setter.
bool isGetter;
if (clangDecl->parameters().size() == 0)
if (clangDecl->parameters().empty())
isGetter = true;
else if (clangDecl->parameters().size() == 1)
isGetter = false;
Expand Down
2 changes: 1 addition & 1 deletion lib/Driver/ToolChain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ ToolChain::constructBatchJob(ArrayRef<const Job *> jobs,
}
}
#endif
if (jobs.size() == 0)
if (jobs.empty())
return nullptr;

// Synthetic OutputInfo is a slightly-modified version of the initial
Expand Down
2 changes: 1 addition & 1 deletion lib/Frontend/ArgsToFrontendOutputsConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ SupplementaryOutputPathsComputer::getSupplementaryFilenamesFromArguments(
if (paths.size() == N)
return paths;

if (paths.size() == 0)
if (paths.empty())
return std::vector<std::string>(N, std::string());

Diags.diagnose(SourceLoc(), diag::error_wrong_number_of_arguments,
Expand Down
4 changes: 2 additions & 2 deletions lib/IDE/Refactoring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2097,7 +2097,7 @@ findConvertToTernaryExpression(ResolvedRangeInfo Info) {
&& Info.Kind != RangeKind::MultiStatement)
return notFound;

if (Info.ContainedNodes.size() == 0)
if (Info.ContainedNodes.empty())
return notFound;

struct AssignExprFinder: public SourceEntityWalker {
Expand Down Expand Up @@ -2529,7 +2529,7 @@ static CharSourceRange
ContextFinder Finder(*TheFile, Node, NodeChecker);
Finder.resolve();
auto Contexts = Finder.getContexts();
if (Contexts.size() == 0)
if (Contexts.empty())
return CharSourceRange();
auto TargetNode = Contexts.back();
BraceStmt *BStmt = dyn_cast<BraceStmt>(TargetNode.dyn_cast<Stmt*>());
Expand Down
4 changes: 2 additions & 2 deletions lib/IDE/TypeReconstruction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1208,7 +1208,7 @@ static void VisitNodeFunction(
auto name = child->getChild(1); // First child is number.
FindNamedDecls(ast, ast->getIdentifier(name->getText()),
decl_scope_result);
if (decl_scope_result._decls.size() == 0) {
if (decl_scope_result._decls.empty()) {
llvm::raw_string_ostream OS(result._error);
OS << "demangled identifier " << name->getText()
<< " could not be found by name lookup";
Expand All @@ -1231,7 +1231,7 @@ static void VisitNodeFunction(
case Demangle::Node::Kind::PostfixOperator:
FindNamedDecls(ast, ast->getIdentifier((*pos)->getText()),
decl_scope_result);
if (decl_scope_result._decls.size() == 0) {
if (decl_scope_result._decls.empty()) {
result._error = stringWithFormat(
"demangled identifier %s could not be found by name lookup",
(*pos)->getText().str().c_str());
Expand Down
8 changes: 4 additions & 4 deletions lib/IRGen/GenCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2920,7 +2920,7 @@ llvm::Value* IRGenFunction::coerceValue(llvm::Value *value, llvm::Type *toTy,

void IRGenFunction::emitScalarReturn(llvm::Type *resultType,
Explosion &result) {
if (result.size() == 0) {
if (result.empty()) {
Builder.CreateRetVoid();
return;
}
Expand Down Expand Up @@ -3008,7 +3008,7 @@ Explosion NativeConventionSchema::mapFromNative(IRGenModule &IGM,
IRGenFunction &IGF,
Explosion &native,
SILType type) const {
if (native.size() == 0) {
if (native.empty()) {
assert(empty() && "Empty explosion must match the native convention");
return Explosion();
}
Expand Down Expand Up @@ -3142,7 +3142,7 @@ Explosion NativeConventionSchema::mapIntoNative(IRGenModule &IGM,
Explosion &fromNonNative,
SILType type,
bool isOutlined) const {
if (fromNonNative.size() == 0) {
if (fromNonNative.empty()) {
assert(empty() && "Empty explosion must match the native convention");
return Explosion();
}
Expand Down Expand Up @@ -3275,7 +3275,7 @@ Explosion NativeConventionSchema::mapIntoNative(IRGenModule &IGM,

void IRGenFunction::emitScalarReturn(SILType resultType, Explosion &result,
bool isSwiftCCReturn, bool isOutlined) {
if (result.size() == 0) {
if (result.empty()) {
assert(IGM.getTypeInfo(resultType).nativeReturnValueSchema(IGM).empty() &&
"Empty explosion must match the native calling convention");

Expand Down
6 changes: 3 additions & 3 deletions lib/IRGen/GenEnum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3591,7 +3591,7 @@ namespace {
auto tagVal = llvm::ConstantInt::get(C, APInt(numTagBits, tagIndex));

// If the payload is empty, there's only one case per tag.
if (CommonSpareBits.size() == 0) {
if (CommonSpareBits.empty()) {
auto found = destMap.find(elti->decl);
if (found != destMap.end())
tagSwitch->addCase(tagVal, found->second);
Expand Down Expand Up @@ -3716,7 +3716,7 @@ namespace {
const LoadableTypeInfo &payloadTI,
Explosion &out) const {
// If the payload is empty, so is the explosion.
if (CommonSpareBits.size() == 0)
if (CommonSpareBits.empty())
return;

// If we have spare bits, we have to mask out any set tag bits packed
Expand Down Expand Up @@ -5243,7 +5243,7 @@ EnumImplStrategy::get(TypeConverter &TC, SILType type, EnumDecl *theEnum) {

// Enums imported from Clang or marked with @objc use C-compatible layout.
if (theEnum->hasClangNode() || theEnum->isObjC()) {
assert(elementsWithPayload.size() == 0 && "C enum with payload?!");
assert(elementsWithPayload.empty() && "C enum with payload?!");
assert(alwaysFixedSize == IsFixedSize && "C enum with resilient payload?!");
return std::unique_ptr<EnumImplStrategy>(
new CCompatibleEnumImplStrategy(TC.IGM, tik, alwaysFixedSize,
Expand Down
4 changes: 2 additions & 2 deletions lib/IRGen/GenFunc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,7 @@ static bool isABIIgnoredParameterWithoutStorage(IRGenModule &IGM,
getArgumentLoweringType(argType.getSwiftRValueType(), param);
auto &ti = IGF.getTypeInfoForLowered(argLoweringTy);
// Empty values don't matter.
return ti.getSchema().size() == 0 && !param.isFormalIndirect();
return ti.getSchema().empty() && !param.isFormalIndirect();
}

/// Find the parameter index for the one (assuming there was only one) partially
Expand Down Expand Up @@ -1374,7 +1374,7 @@ void irgen::emitFunctionPartialApplication(

// Empty values don't matter.
auto schema = ti.getSchema();
if (schema.size() == 0 && !param.isFormalIndirect())
if (schema.empty() && !param.isFormalIndirect())
continue;

argValTypes.push_back(argType);
Expand Down
2 changes: 1 addition & 1 deletion lib/IRGen/IRGenDebugInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1951,7 +1951,7 @@ void IRGenDebugInfoImpl::emitVariableDeclaration(
}

// Emit locationless intrinsic for variables that were optimized away.
if (Storage.size() == 0)
if (Storage.empty())
emitDbgIntrinsic(Builder, llvm::ConstantInt::get(IGM.Int64Ty, 0), Var,
DBuilder.createExpression(), Line, Loc.Column, Scope, DS);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/LLVMPasses/LLVMInlineTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class InlineTree {
bool isTopLevel = false;

const NodeList &getChildren() {
if (SortedChildren.size() == 0 && UnsortedChildren.size() > 0)
if (SortedChildren.empty() && UnsortedChildren.size() > 0)
sortNodes(UnsortedChildren, SortedChildren);
return SortedChildren;
}
Expand Down Expand Up @@ -274,7 +274,7 @@ void InlineTree::printNode(Node *Nd, int indent, raw_ostream &os) {
void InlineTree::build(Module *M) {
// Build the trees for all top-level functions.
for (Function &F : *M) {
if (F.size() == 0)
if (F.empty())
continue;
buildTree(&F);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Parse/Lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ void Lexer::formEscapedIdentifierToken(const char *TokStart) {
assert(CurPtr[-1] == '`' && "escaped identifier ends with backtick");
if (TriviaRetention == TriviaRetentionMode::WithTrivia) {
LeadingTrivia.push_back(TriviaPiece::backtick());
assert(TrailingTrivia.size() == 0 && "TrailingTrivia is empty here");
assert(TrailingTrivia.empty() && "TrailingTrivia is empty here");
TrailingTrivia.push_back(TriviaPiece::backtick());
}
formToken(tok::identifier, TokStart);
Expand Down
2 changes: 1 addition & 1 deletion lib/PrintAsObjC/PrintAsObjC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,
}

// Zero-parameter methods.
if (params.size() == 0) {
if (params.empty()) {
assert(paramIndex == 0);
os << piece;
paramIndex = 1;
Expand Down
4 changes: 2 additions & 2 deletions lib/SIL/SILInstructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ AllocRefInstBase::AllocRefInstBase(SILInstructionKind Kind,
SILInstruction::Bits.AllocRefInstBase.NumTailTypes = ElementTypes.size();
assert(SILInstruction::Bits.AllocRefInstBase.NumTailTypes ==
ElementTypes.size() && "Truncation");
assert(!objc || ElementTypes.size() == 0);
assert(!objc || ElementTypes.empty());
}

AllocRefInst *AllocRefInst::create(SILDebugLocation Loc, SILFunction &F,
Expand All @@ -199,7 +199,7 @@ AllocRefInst *AllocRefInst::create(SILDebugLocation Loc, SILFunction &F,
ArrayRef<SILValue> ElementCountOperands,
SILOpenedArchetypesState &OpenedArchetypes) {
assert(ElementTypes.size() == ElementCountOperands.size());
assert(!objc || ElementTypes.size() == 0);
assert(!objc || ElementTypes.empty());
SmallVector<SILValue, 8> AllOperands(ElementCountOperands.begin(),
ElementCountOperands.end());
for (SILType ElemType : ElementTypes) {
Expand Down
8 changes: 4 additions & 4 deletions lib/SIL/SILVerifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3482,7 +3482,7 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
"switch_enum destination for case w/ args must take 1 "
"argument");
} else {
require(dest->getArguments().size() == 0 ||
require(dest->getArguments().empty() ||
dest->getArguments().size() == 1,
"switch_enum destination for case w/ args must take 0 or 1 "
"arguments");
Expand All @@ -3502,7 +3502,7 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
}

} else {
require(dest->getArguments().size() == 0,
require(dest->getArguments().empty(),
"switch_enum destination for no-argument case must take no "
"arguments");
}
Expand Down Expand Up @@ -3560,7 +3560,7 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
unswitchedElts.erase(elt);

// The destination BB must not have BB arguments.
require(dest->getArguments().size() == 0,
require(dest->getArguments().empty(),
"switch_enum_addr destination must take no BB args");
}

Expand Down Expand Up @@ -4704,7 +4704,7 @@ void SILWitnessTable::verify(const SILModule &M) const {
return;
#endif
if (isDeclaration())
assert(getEntries().size() == 0 &&
assert(getEntries().empty() &&
"A witness table declaration should not have any entries.");

auto *protocol = getConformance()->getProtocol();
Expand Down
2 changes: 1 addition & 1 deletion lib/SILOptimizer/Analysis/SideEffectAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ void SideEffectAnalysis::recompute(FunctionInfo *Initial) {
}

void SideEffectAnalysis::getEffects(FunctionEffects &ApplyEffects, FullApplySite FAS) {
assert(ApplyEffects.ParamEffects.size() == 0 &&
assert(ApplyEffects.ParamEffects.empty() &&
"Not using a new ApplyEffects?");
ApplyEffects.ParamEffects.resize(FAS.getNumArguments());

Expand Down
4 changes: 2 additions & 2 deletions lib/SILOptimizer/Mandatory/DiagnoseStaticExclusivity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -905,7 +905,7 @@ static void checkForViolationWithCall(
const auto &SubAccesses = AS.getSubAccesses();

// Is the capture accessed in the callee?
if (SubAccesses.size() == 0)
if (SubAccesses.empty())
continue;

SILValue Argument = Arguments[ArgumentIndex];
Expand Down Expand Up @@ -1297,7 +1297,7 @@ static void checkStaticExclusivity(SILFunction &Fn, PostOrderFunctionInfo *PO,
}
#endif
// Sanity check to make sure entries are properly removed.
assert((!isa<ReturnInst>(&I) || Accesses.size() == 0) &&
assert((!isa<ReturnInst>(&I) || Accesses.empty()) &&
"Entries were not properly removed?!");
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/SILOptimizer/Transforms/CopyForwarding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1223,7 +1223,7 @@ void CopyForwarding::forwardCopiesOf(SILValue Def, SILFunction *F) {
for (auto *BB : PostOrder->get(F)->getPostOrder()) {
SmallVector<unsigned, 4> DeadInSuccs;
ArrayRef<SILSuccessor> Succs = BB->getSuccessors();
if (Succs.size() == 0)
if (Succs.empty())
continue;

for (unsigned EdgeIdx = 0, End = Succs.size(); EdgeIdx != End; ++EdgeIdx) {
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/CSDiag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4465,7 +4465,7 @@ bool FailureDiagnosis::diagnoseParameterErrors(CalleeCandidateInfo &CCI,
if (instTy->getAnyNominal()) {
// If we are invoking a constructor on a nominal type and there are
// absolutely no candidates, then they must all be private.
if (CCI.size() == 0 || (CCI.size() == 1 && CCI.candidates[0].getDecl() &&
if (CCI.empty() || (CCI.size() == 1 && CCI.candidates[0].getDecl() &&
isa<ProtocolDecl>(CCI.candidates[0].getDecl()))) {
CS.TC.diagnose(fnExpr->getLoc(), diag::no_accessible_initializers,
instTy);
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/TypeCheckAvailability.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ class TypeRefinementContextBuilder : private ASTWalker {
// If this query expression has no queries, we will not introduce a new
// refinement context. We do not diagnose here: a diagnostic will already
// have been emitted by the parser.
if (Query->getQueries().size() == 0)
if (Query->getQueries().empty())
continue;

AvailabilitySpec *Spec = bestActiveSpecForQuery(Query);
Expand Down