Skip to content

Commit 286b18f

Browse files
committed
Address Jordan's code review comments.
1 parent 7beb451 commit 286b18f

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

lib/Sema/MiscDiagnostics.cpp

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2508,10 +2508,11 @@ void swift::performAbstractFuncDeclDiagnostics(TypeChecker &TC,
25082508

25092509
/// Diagnose C style for loops.
25102510

2511-
enum OperatorKind {
2511+
namespace {
2512+
enum class OperatorKind {
25122513
Greater,
25132514
Smaller,
2514-
NEqual,
2515+
NotEqual,
25152516
};
25162517

25172518
static Expr *endConditionValueForConvertingCStyleForLoop(const ForStmt *FS,
@@ -2535,7 +2536,7 @@ static Expr *endConditionValueForConvertingCStyleForLoop(const ForStmt *FS,
25352536
// Verify that the condition is a simple != or < comparison to the loop variable.
25362537
auto comparisonOpName = binaryFuncExpr->getDecl()->getNameStr();
25372538
if (comparisonOpName == "!=")
2538-
OpKind = OperatorKind::NEqual;
2539+
OpKind = OperatorKind::NotEqual;
25392540
else if (comparisonOpName == "<")
25402541
OpKind = OperatorKind::Smaller;
25412542
else if (comparisonOpName == ">")
@@ -2557,7 +2558,7 @@ static Expr *endConditionValueForConvertingCStyleForLoop(const ForStmt *FS,
25572558

25582559
static bool unaryOperatorCheckForConvertingCStyleForLoop(const ForStmt *FS,
25592560
VarDecl *loopVar,
2560-
StringRef OpContent) {
2561+
StringRef OpName) {
25612562
auto *Increment = FS->getIncrement().getPtrOrNull();
25622563
if (!Increment)
25632564
return false;
@@ -2575,7 +2576,7 @@ static bool unaryOperatorCheckForConvertingCStyleForLoop(const ForStmt *FS,
25752576
auto unaryFuncExpr = dyn_cast<DeclRefExpr>(unaryExpr->getFn());
25762577
if (!unaryFuncExpr)
25772578
return false;
2578-
if (unaryFuncExpr->getDecl()->getNameStr() != OpContent)
2579+
if (unaryFuncExpr->getDecl()->getNameStr() != OpName)
25792580
return false;
25802581
return incrementDeclRefExpr->getDecl() == loopVar;
25812582
}
@@ -2594,7 +2595,7 @@ static bool unaryDecrementForConvertingCStyleForLoop(const ForStmt *FS,
25942595
static bool binaryOperatorCheckForConvertingCStyleForLoop(TypeChecker &TC,
25952596
const ForStmt *FS,
25962597
VarDecl *loopVar,
2597-
StringRef OpContent) {
2598+
StringRef OpName) {
25982599
auto *Increment = FS->getIncrement().getPtrOrNull();
25992600
if (!Increment)
26002601
return false;
@@ -2604,7 +2605,7 @@ static bool binaryOperatorCheckForConvertingCStyleForLoop(TypeChecker &TC,
26042605
auto binaryFuncExpr = dyn_cast<DeclRefExpr>(binaryExpr->getFn());
26052606
if (!binaryFuncExpr)
26062607
return false;
2607-
if (binaryFuncExpr->getDecl()->getNameStr() != OpContent)
2608+
if (binaryFuncExpr->getDecl()->getNameStr() != OpName)
26082609
return false;
26092610
auto argTupleExpr = dyn_cast<TupleExpr>(binaryExpr->getArg());
26102611
if (!argTupleExpr)
@@ -2690,11 +2691,11 @@ static void checkCStyleForLoop(TypeChecker &TC, const ForStmt *FS) {
26902691

26912692
if (strideByOne && OpKind != OperatorKind::Greater) {
26922693
diagnostic
2693-
.fixItRemoveChars(loopVarDecl->getLoc(), loopVar->getLoc())
2694-
.fixItReplaceChars(loopPatternEnd, startValue->getStartLoc(), " in ")
2695-
.fixItReplaceChars(FS->getFirstSemicolonLoc(), endValue->getStartLoc(),
2694+
.fixItRemoveChars(loopVarDecl->getLoc(), loopVar->getLoc())
2695+
.fixItReplaceChars(loopPatternEnd, startValue->getStartLoc(), " in ")
2696+
.fixItReplaceChars(FS->getFirstSemicolonLoc(), endValue->getStartLoc(),
26962697
" ..< ")
2697-
.fixItRemoveChars(FS->getSecondSemicolonLoc(), endOfIncrementLoc);
2698+
.fixItRemoveChars(FS->getSecondSemicolonLoc(), endOfIncrementLoc);
26982699
return;
26992700
} else if (strideBackByOne && OpKind != OperatorKind::Smaller) {
27002701
SourceLoc startValueEnd = Lexer::getLocForEndOfToken(TC.Context.SourceMgr,
@@ -2704,14 +2705,14 @@ static void checkCStyleForLoop(TypeChecker &TC, const ForStmt *FS) {
27042705
Lexer::getLocForEndOfToken(TC.Context.SourceMgr, endValue->getEndLoc())).str();
27052706

27062707
diagnostic
2707-
.fixItRemoveChars(loopVarDecl->getLoc(), loopVar->getLoc())
2708-
.fixItReplaceChars(loopPatternEnd, startValue->getStartLoc(), " in ")
2709-
.fixItInsert(startValue->getStartLoc(), (llvm::Twine("((") + endValueStr + " + 1)...").str())
2710-
.fixItInsert(startValueEnd, ").reversed()")
2711-
.fixItRemoveChars(FS->getFirstSemicolonLoc(), endOfIncrementLoc);
2708+
.fixItRemoveChars(loopVarDecl->getLoc(), loopVar->getLoc())
2709+
.fixItReplaceChars(loopPatternEnd, startValue->getStartLoc(), " in ")
2710+
.fixItInsert(startValue->getStartLoc(), (llvm::Twine("((") + endValueStr + " + 1)...").str())
2711+
.fixItInsert(startValueEnd, ").reversed()")
2712+
.fixItRemoveChars(FS->getFirstSemicolonLoc(), endOfIncrementLoc);
27122713
}
27132714
}
2714-
2715+
}// Anonymous namespace end.
27152716

27162717
// Perform MiscDiagnostics on Switch Statements.
27172718
static void checkSwitch(TypeChecker &TC, const SwitchStmt *stmt) {

test/Sema/diag_c_style_for.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ for var f = 3; f < 4; f-- { // expected-error {{C-style for statement has been r
2828
for var i = 6; i > 0; i-=1 { // expected-error {{C-style for statement has been removed in Swift 3}} {{5-9=}} {{10-13= in }} {{13-13=((0 + 1)...}} {{14-14=).reversed()}} {{14-27=}}
2929
}
3030

31+
for var i = 100; i != 0; i-=1 { // expected-error {{C-style for statement has been removed in Swift 3}} {{5-9=}} {{10-13= in }} {{13-13=((0 + 1)...}} {{16-16=).reversed()}} {{16-30=}}
32+
}
33+
3134
let start = Int8(4)
3235
let count = Int8(10)
3336
var other = Int8(2)

0 commit comments

Comments
 (0)