@@ -7318,11 +7318,12 @@ bool FailureDiagnosis::visitArrayExpr(ArrayExpr *E) {
7318
7318
}
7319
7319
}
7320
7320
}
7321
-
7321
+
7322
+ auto numElements = E->getNumElements ();
7322
7323
if (!Conformance) {
7323
7324
// If the contextual type conforms to ExpressibleByDictionaryLiteral and
7324
7325
// this is an empty array, then they meant "[:]".
7325
- if (E-> getNumElements () == 0 &&
7326
+ if (numElements == 0 &&
7326
7327
isDictionaryLiteralCompatible (contextualType, CS, E->getLoc ())) {
7327
7328
diagnose (E->getStartLoc (), diag::should_use_empty_dictionary_literal)
7328
7329
.fixItInsert (E->getEndLoc (), " :" );
@@ -7335,13 +7336,19 @@ bool FailureDiagnosis::visitArrayExpr(ArrayExpr *E) {
7335
7336
7336
7337
// If the contextual type conforms to ExpressibleByDictionaryLiteral, then
7337
7338
// they wrote "x = [1,2]" but probably meant "x = [1:2]".
7338
- if ((E-> getElements (). size () & 1 ) == 0 && !E-> getElements (). empty () &&
7339
+ if ((numElements & 1 ) == 0 && numElements > 0 &&
7339
7340
isDictionaryLiteralCompatible (contextualType, CS, E->getLoc ())) {
7340
7341
auto diag = diagnose (E->getStartLoc (), diag::meant_dictionary_lit);
7341
7342
7342
- // Change every other comma into a colon.
7343
- for (unsigned i = 0 , e = E->getElements ().size ()/2 ; i != e; ++i)
7344
- diag.fixItReplace (E->getCommaLocs ()[i*2 ], " :" );
7343
+ // Change every other comma into a colon, only if the number
7344
+ // of commas present matches the number of elements, because
7345
+ // otherwise it might a structural problem with the expression
7346
+ // e.g. ["a""b": 1].
7347
+ const auto commaLocs = E->getCommaLocs ();
7348
+ if (commaLocs.size () == numElements - 1 ) {
7349
+ for (unsigned i = 0 , e = numElements / 2 ; i != e; ++i)
7350
+ diag.fixItReplace (commaLocs[i*2 ], " :" );
7351
+ }
7345
7352
}
7346
7353
7347
7354
return true ;
0 commit comments