Skip to content

Commit dd813af

Browse files
authored
Merge pull request #65445 from xedin/rdar-108064941
[Diagnostics] Suppress unused expression warning if result is discard…
2 parents 421042e + dc8c05a commit dd813af

File tree

4 files changed

+33
-0
lines changed

4 files changed

+33
-0
lines changed

include/swift/AST/Types.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2459,6 +2459,10 @@ class TupleType final : public TypeBase, public llvm::FoldingSetNode,
24592459

24602460
bool containsPackExpansionType() const;
24612461

2462+
/// Check whether this tuple consists of a single unlabeled element
2463+
/// of \c PackExpansionType.
2464+
bool isSingleUnlabeledPackExpansion() const;
2465+
24622466
private:
24632467
TupleType(ArrayRef<TupleTypeElt> elements, const ASTContext *CanCtx,
24642468
RecursiveTypeProperties properties)

lib/AST/ParameterPack.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,14 @@ bool CanTupleType::containsPackExpansionTypeImpl(CanTupleType tuple) {
134134
return false;
135135
}
136136

137+
bool TupleType::isSingleUnlabeledPackExpansion() const {
138+
if (getNumElements() != 1)
139+
return false;
140+
141+
const auto &elt = getElement(0);
142+
return !elt.hasName() && elt.getType()->is<PackExpansionType>();
143+
}
144+
137145
bool AnyFunctionType::containsPackExpansionType(ArrayRef<Param> params) {
138146
for (auto param : params) {
139147
if (param.getPlainType()->is<PackExpansionType>())

lib/Sema/TypeCheckStmt.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1749,6 +1749,16 @@ Stmt *PreCheckReturnStmtRequest::evaluate(Evaluator &evaluator, ReturnStmt *RS,
17491749
}
17501750

17511751
static bool isDiscardableType(Type type) {
1752+
// If type is `(_: repeat ...)`, it can be discardable.
1753+
if (auto *tuple = type->getAs<TupleType>()) {
1754+
if (tuple->isSingleUnlabeledPackExpansion()) {
1755+
type = tuple->getElementType(0);
1756+
}
1757+
}
1758+
1759+
if (auto *expansion = type->getAs<PackExpansionType>())
1760+
return isDiscardableType(expansion->getPatternType());
1761+
17521762
return (type->hasError() ||
17531763
type->isUninhabited() ||
17541764
type->lookThroughAllOptionalTypes()->isVoid());

test/Constraints/pack-expansion-expressions.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,3 +397,14 @@ do {
397397
_ = Defaulted(t: "b", 0) // Ok
398398
_ = Defaulted(t: "c", 1.0, u: "d", 0) // Ok
399399
}
400+
401+
// rdar://108064941 - unused result diagnostic is unaware of Void packs
402+
func test_no_unused_result_warning(arr: inout [Any]) {
403+
func test1<each T>(_ value: (repeat each T)) {
404+
repeat arr.append(each value.element) // no warning
405+
}
406+
407+
func test2<each T>(_ value: repeat each T) {
408+
((repeat arr.append(each value))) // no warning
409+
}
410+
}

0 commit comments

Comments
 (0)