-
Notifications
You must be signed in to change notification settings - Fork 15.1k
[clang-format][NFC] Replace size() with empty() #147164
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
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
@llvm/pr-subscribers-clang-format Author: Owen Pan (owenca) ChangesFull diff: https://github.com/llvm/llvm-project/pull/147164.diff 5 Files Affected:
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index bdaf264e9adce..f0412cddc6f19 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -3582,7 +3582,7 @@ tooling::Replacements sortJavaImports(const FormatStyle &Style, StringRef Code,
ImportsInBlock.push_back(
{Identifier, Line, Prev, AssociatedCommentLines, IsStatic});
AssociatedCommentLines.clear();
- } else if (Trimmed.size() > 0 && !ImportsInBlock.empty()) {
+ } else if (!Trimmed.empty() && !ImportsInBlock.empty()) {
// Associating comments within the imports with the nearest import below
AssociatedCommentLines.push_back(Line);
}
diff --git a/clang/lib/Format/FormatTokenLexer.cpp b/clang/lib/Format/FormatTokenLexer.cpp
index 06f68ec8b0fc1..05dee26735cc9 100644
--- a/clang/lib/Format/FormatTokenLexer.cpp
+++ b/clang/lib/Format/FormatTokenLexer.cpp
@@ -1371,8 +1371,7 @@ FormatToken *FormatTokenLexer::getNextToken() {
} else if (FormatTok->TokenText == "``") {
FormatTok->Tok.setIdentifierInfo(nullptr);
FormatTok->Tok.setKind(tok::hashhash);
- } else if (Tokens.size() > 0 &&
- Tokens.back()->is(Keywords.kw_apostrophe) &&
+ } else if (!Tokens.empty() && Tokens.back()->is(Keywords.kw_apostrophe) &&
NumberBase.match(FormatTok->TokenText, &Matches)) {
// In Verilog in a based number literal like `'b10`, there may be
// whitespace between `'b` and `10`. Therefore we handle the base and
@@ -1420,7 +1419,7 @@ FormatToken *FormatTokenLexer::getNextToken() {
tryParseJavaTextBlock();
}
- if (Style.isVerilog() && Tokens.size() > 0 &&
+ if (Style.isVerilog() && !Tokens.empty() &&
Tokens.back()->is(TT_VerilogNumberBase) &&
FormatTok->Tok.isOneOf(tok::identifier, tok::question)) {
// Mark the number following a base like `'h?a0` as a number.
@@ -1454,9 +1453,9 @@ FormatToken *FormatTokenLexer::getNextToken() {
if (Style.isCpp()) {
auto *Identifier = FormatTok->Tok.getIdentifierInfo();
auto it = Macros.find(Identifier);
- if (!(Tokens.size() > 0 && Tokens.back()->Tok.getIdentifierInfo() &&
- Tokens.back()->Tok.getIdentifierInfo()->getPPKeywordID() ==
- tok::pp_define) &&
+ if ((Tokens.empty() || !Tokens.back()->Tok.getIdentifierInfo() ||
+ Tokens.back()->Tok.getIdentifierInfo()->getPPKeywordID() !=
+ tok::pp_define) &&
it != Macros.end()) {
FormatTok->setType(it->second);
if (it->second == TT_IfMacro) {
diff --git a/clang/lib/Format/QualifierAlignmentFixer.cpp b/clang/lib/Format/QualifierAlignmentFixer.cpp
index b0dda65adfba1..441a37a4902b7 100644
--- a/clang/lib/Format/QualifierAlignmentFixer.cpp
+++ b/clang/lib/Format/QualifierAlignmentFixer.cpp
@@ -640,7 +640,7 @@ bool isPossibleMacro(const FormatToken *Tok) {
return false;
const auto Text = Tok->TokenText;
- assert(Text.size() > 0);
+ assert(!Text.empty());
// T,K,U,V likely could be template arguments
if (Text.size() == 1)
diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp
index ac8c0c8fd0ec0..0adf7ee9ed543 100644
--- a/clang/lib/Format/UnwrappedLineFormatter.cpp
+++ b/clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -223,8 +223,6 @@ class LineJoiner {
tryFitMultipleLinesInOne(LevelIndentTracker &IndentTracker,
ArrayRef<AnnotatedLine *>::const_iterator I,
ArrayRef<AnnotatedLine *>::const_iterator E) {
- const unsigned Indent = IndentTracker.getIndent();
-
// Can't join the last line with anything.
if (I + 1 == E)
return 0;
@@ -240,6 +238,7 @@ class LineJoiner {
return 0;
}
+ const auto Indent = IndentTracker.getIndent();
if (Style.ColumnLimit > 0 && Indent > Style.ColumnLimit)
return 0;
@@ -1090,7 +1089,7 @@ class LineFormatter {
const FormatToken *LBrace = State.NextToken->getPreviousNonComment();
bool HasLBrace = LBrace && LBrace->is(tok::l_brace) && LBrace->is(BK_Block);
FormatToken &Previous = *State.NextToken->Previous;
- if (Previous.Children.size() == 0 || (!HasLBrace && !LBrace->MacroParent)) {
+ if (Previous.Children.empty() || (!HasLBrace && !LBrace->MacroParent)) {
// The previous token does not open a block. Nothing to do. We don't
// assert so that we can simply call this function for all tokens.
return true;
diff --git a/clang/lib/Format/WhitespaceManager.cpp b/clang/lib/Format/WhitespaceManager.cpp
index cfaadf07edfd0..cc3cc0f6906cc 100644
--- a/clang/lib/Format/WhitespaceManager.cpp
+++ b/clang/lib/Format/WhitespaceManager.cpp
@@ -314,7 +314,7 @@ AlignTokenSequence(const FormatStyle &Style, unsigned Start, unsigned End,
for (unsigned i = Start; i != End; ++i) {
auto &CurrentChange = Changes[i];
- if (ScopeStack.size() != 0 &&
+ if (!ScopeStack.empty() &&
CurrentChange.indentAndNestingLevel() <
Changes[ScopeStack.back()].indentAndNestingLevel()) {
ScopeStack.pop_back();
@@ -332,7 +332,7 @@ AlignTokenSequence(const FormatStyle &Style, unsigned Start, unsigned End,
ScopeStack.push_back(i);
}
- bool InsideNestedScope = ScopeStack.size() != 0;
+ bool InsideNestedScope = !ScopeStack.empty();
bool ContinuedStringLiteral = i > Start &&
CurrentChange.Tok->is(tok::string_literal) &&
Changes[i - 1].Tok->is(tok::string_literal);
@@ -1048,7 +1048,7 @@ void WhitespaceManager::alignChainedConditionals() {
return C.Tok->is(TT_ConditionalExpr) &&
((C.Tok->is(tok::question) && !C.NewlinesBefore) ||
(C.Tok->is(tok::colon) && C.Tok->Next &&
- (C.Tok->Next->FakeLParens.size() == 0 ||
+ (C.Tok->Next->FakeLParens.empty() ||
C.Tok->Next->FakeLParens.back() != prec::Conditional)));
},
Changes, /*StartAt=*/0);
@@ -1057,7 +1057,7 @@ void WhitespaceManager::alignChainedConditionals() {
FormatToken *Previous = C.Tok->getPreviousNonComment();
return C.NewlinesBefore && Previous && Previous->is(TT_ConditionalExpr) &&
(Previous->is(tok::colon) &&
- (C.Tok->FakeLParens.size() == 0 ||
+ (C.Tok->FakeLParens.empty() ||
C.Tok->FakeLParens.back() != prec::Conditional));
};
// Ensure we keep alignment of wrapped operands with non-wrapped operands
|
HazardyKnusperkeks
approved these changes
Jul 6, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
No description provided.