diff --git a/include/swift/AST/DiagnosticsSema.def b/include/swift/AST/DiagnosticsSema.def index 183ebdc3c3227..15f7af5f8ea1b 100644 --- a/include/swift/AST/DiagnosticsSema.def +++ b/include/swift/AST/DiagnosticsSema.def @@ -2192,10 +2192,16 @@ ERROR(expression_unused_function,none, "expression resolves to an unused function", ()) ERROR(expression_unused_lvalue,none, "expression resolves to an unused l-value", ()) -WARNING(expression_unused_result,none, +WARNING(expression_unused_result_call,none, "result of call to %0 is unused", (DeclName)) +WARNING(expression_unused_result_operator,none, + "result of operator %0 is unused", (DeclName)) +WARNING(expression_unused_result_unknown, none, + "result of call is unused, but produces %0", (Type)) +WARNING(expression_unused_result, none, + "expression of type %0 is unused", (Type)) WARNING(expression_unused_init_result,none, - "result of initializer is unused", ()) + "result of %0 initializer is unused", (Type)) WARNING(expression_unused_optional_try,none, "result of 'try?' is unused", ()) WARNING(expression_unused_selector_result, none, diff --git a/lib/Sema/TypeCheckStmt.cpp b/lib/Sema/TypeCheckStmt.cpp index ea15cc105b6c3..bfc590ecdd457 100644 --- a/lib/Sema/TypeCheckStmt.cpp +++ b/lib/Sema/TypeCheckStmt.cpp @@ -1015,6 +1015,17 @@ void TypeChecker::checkIgnoredExpr(Expr *E) { return; } + // Drill through noop expressions we don't care about, like ParanExprs. + auto valueE = E; + while (1) { + valueE = valueE->getValueProvidingExpr(); + + if (auto *OEE = dyn_cast(valueE)) + valueE = OEE->getSubExpr(); + else + break; + } + // Complain about functions that aren't called. // TODO: What about tuples which contain functions by-value that are // dead? @@ -1024,7 +1035,11 @@ void TypeChecker::checkIgnoredExpr(Expr *E) { return; } - auto valueE = E->getValueProvidingExpr(); + // If the result of this expression is of type "()", then it is safe to + // ignore. + if (valueE->getType()->isVoid() || + valueE->getType()->is()) + return; // Complain about '#selector'. if (auto *ObjCSE = dyn_cast(valueE)) { @@ -1047,15 +1062,20 @@ void TypeChecker::checkIgnoredExpr(Expr *E) { if (auto *IIO = dyn_cast(OEE->getSubExpr())) return checkIgnoredExpr(IIO->getSubExpr()); - // FIXME: Complain about literals - // Check if we have a call to a function not marked with // '@discardableResult'. if (auto call = dyn_cast(valueE)) { // Dig through all levels of calls. - Expr *fn = call->getFn()->getSemanticsProvidingExpr(); - while (auto applyFn = dyn_cast(fn)) { - fn = applyFn->getFn()->getSemanticsProvidingExpr(); + Expr *fn = call->getFn(); + while (true) { + fn = fn->getSemanticsProvidingExpr(); + if (auto applyFn = dyn_cast(fn)) { + fn = applyFn->getFn(); + } else if (auto FVE = dyn_cast(fn)) { + fn = FVE->getSubExpr(); + } else { + break; + } } // Find the callee. @@ -1069,19 +1089,45 @@ void TypeChecker::checkIgnoredExpr(Expr *E) { else if (auto dynMemberRef = dyn_cast(fn)) callee = dyn_cast( dynMemberRef->getMember().getDecl()); - - if (callee) { - if (!callee->getAttrs().getAttribute() && - !valueE->getType()->isVoid()) { - diagnose(fn->getLoc(), diag::expression_unused_result, - callee->getFullName()); - return; - } - if (isa(callee) && !call->isImplicit()) { - diagnose(fn->getLoc(), diag::expression_unused_init_result); - } + + // If the callee explicitly allows its result to be ignored, then don't + // complain. + if (callee && callee->getAttrs().getAttribute()) + return; + + // Otherwise, complain. Start with more specific diagnostics. + if (callee && isa(callee) && !call->isImplicit()) { + diagnose(fn->getLoc(), diag::expression_unused_init_result, + callee->getDeclContext()->getDeclaredTypeOfContext()) + .highlight(call->getArg()->getSourceRange()); + return; } + + SourceRange SR1 = call->getArg()->getSourceRange(), SR2; + if (auto *BO = dyn_cast(call)) { + SR1 = BO->getArg()->getElement(0)->getSourceRange(); + SR2 = BO->getArg()->getElement(1)->getSourceRange(); + } + + // Otherwise, produce a generic diagnostic. + if (callee) { + auto diagID = diag::expression_unused_result_call; + if (callee->getFullName().isOperator()) + diagID = diag::expression_unused_result_operator; + + diagnose(fn->getLoc(), diagID, callee->getFullName()) + .highlight(SR1).highlight(SR2); + } else + diagnose(fn->getLoc(), diag::expression_unused_result_unknown, + valueE->getType()) + .highlight(SR1).highlight(SR2); + + return; } + + // Produce a generic diagnostic. + diagnose(valueE->getLoc(), diag::expression_unused_result, valueE->getType()) + .highlight(valueE->getSourceRange()); } Stmt *StmtChecker::visitBraceStmt(BraceStmt *BS) { diff --git a/test/ClangModules/AppKit_test.swift b/test/ClangModules/AppKit_test.swift index 4bd589c595f80..3441362502851 100644 --- a/test/ClangModules/AppKit_test.swift +++ b/test/ClangModules/AppKit_test.swift @@ -15,8 +15,8 @@ class MyDocument : NSDocument { } func test(_ URL: NSURL, controller: NSDocumentController) { - try! NSDocument(contentsOf: URL, ofType: "") // expected-warning{{unused}} - try! MyDocument(contentsOf: URL, ofType: "") + try! NSDocument(contentsOf: URL, ofType: "") // expected-warning{{result of 'NSDocument' initializer is unused}} + try! MyDocument(contentsOf: URL, ofType: "") // expected-warning {{expression of type 'MyDocument' is unused}} try! controller.makeDocument(withContentsOf: URL, ofType: "") } diff --git a/test/ClangModules/adapter.swift b/test/ClangModules/adapter.swift index 32546e6e499f1..4e0386b6968f2 100644 --- a/test/ClangModules/adapter.swift +++ b/test/ClangModules/adapter.swift @@ -15,7 +15,7 @@ import Redeclaration let encoding: UInt = NSUTF8StringEncoding let viaTypedef: Redeclaration.NSPoint = AppKit.NSPoint(x: 0, y: 0) -Redeclaration.NSStringToNSString(AppKit.NSStringToNSString("abc")) +Redeclaration.NSStringToNSString(AppKit.NSStringToNSString("abc")) // expected-warning {{result of call is unused}} let viaStruct: Redeclaration.FooStruct1 = AppKit.FooStruct1() let forwardDecl: Redeclaration.Tribool = AppKit.Tribool() // expected-error {{no type named 'Tribool' in module 'Redeclaration'}} diff --git a/test/ClangModules/availability.swift b/test/ClangModules/availability.swift index ef0e158c8ba6b..f1abae4eb5ca1 100644 --- a/test/ClangModules/availability.swift +++ b/test/ClangModules/availability.swift @@ -14,6 +14,7 @@ func test_unavailable_instance_method(_ x : NSObject) -> Bool { } func test_unavailable_method_in_protocol(_ x : NSObjectProtocol) { + // expected-warning @+1 {{expression of type 'NSObjectProtocol' is unused}} x.retain() // expected-error {{'retain()' is unavailable}} } func test_unavailable_method_in_protocol_use_class_instance(_ x : NSObject) { @@ -34,8 +35,8 @@ func test_NSInvocation(_ x: NSInvocation, // expected-error {{'NSInvocat func test_class_avail(_ x:NSObject, obj: AnyObject) { x.`class`() // expected-error {{'class()' is unavailable in Swift: use 'dynamicType' instead}} expected-warning {{result of call to 'class()' is unused}} - NSObject.`class`() // expected-error {{'class()' is unavailable in Swift: use 'self' instead}} expected-warning {{result of call to 'class()' is unused}} - obj.`class`!() // expected-error {{'class()' is unavailable in Swift: use 'dynamicType' instead}} + _ = NSObject.`class`() // expected-error {{'class()' is unavailable in Swift: use 'self' instead}} + _ = obj.`class`!() // expected-error {{'class()' is unavailable in Swift: use 'dynamicType' instead}} } func test_unavailable_app_extension() { @@ -100,7 +101,7 @@ func test_DistributedObjects(_ o: NSObject, let ca = NSConnectionDidDieNotification // expected-error {{'NSConnectionDidDieNotification' is unavailable in Swift: Use NSXPCConnection instead}} let cc = NSConnectionReplyMode // expected-error {{'NSConnectionReplyMode' is unavailable in Swift: Use NSXPCConnection instead}} - o.classForPortCoder // expected-error {{'classForPortCoder' is unavailable in Swift: Use NSXPCConnection instead}} + _ = o.classForPortCoder // expected-error {{'classForPortCoder' is unavailable in Swift: Use NSXPCConnection instead}} } func test_NSCalendarDate(_ o: NSCalendarDate) {} // expected-error {{'NSCalendarDate' is unavailable in Swift: Use NSCalendar and NSDateComponents and NSDateFormatter instead}} diff --git a/test/ClangModules/foreign_errors.swift b/test/ClangModules/foreign_errors.swift index 09c70cdbcea3c..a5200da39fd51 100644 --- a/test/ClangModules/foreign_errors.swift +++ b/test/ClangModules/foreign_errors.swift @@ -104,7 +104,7 @@ func testNSErrorExhaustive() { do { try ErrorProne.fail() } catch let e as NSError { - e + e // expected-warning {{expression of type 'NSError' is unused}} } } } diff --git a/test/ClangModules/objc_parse.swift b/test/ClangModules/objc_parse.swift index 0e527aadbcdd1..d36e971386e41 100644 --- a/test/ClangModules/objc_parse.swift +++ b/test/ClangModules/objc_parse.swift @@ -49,7 +49,7 @@ func instanceMethods(_ b: B) { b.performAdd(1, 2, 3, 4) // expected-error{{missing argument labels 'withValue:withValue:withValue2:' in call}} {{19-19=withValue: }} {{22-22=withValue: }} {{25-25=withValue2: }} // Both class and instance methods exist. - b.description + _ = b.description b.instanceTakesObjectClassTakesFloat(b) b.instanceTakesObjectClassTakesFloat(2.0) // expected-error{{cannot convert value of type 'Double' to expected argument type 'AnyObject!'}} @@ -86,21 +86,21 @@ func instanceMethodsInExtensions(_ b: B) { b.method(1, separateExtMethod:3.5) let m1 = b.method(_:onCat1:) - m1(1, onCat1: 2.5) + _ = m1(1, onCat1: 2.5) let m2 = b.method(_:onExtA:) - m2(1, onExtA: 2.5) + _ = m2(1, onExtA: 2.5) let m3 = b.method(_:onExtB:) - m3(1, onExtB: 2.5) + _ = m3(1, onExtB: 2.5) let m4 = b.method(_:separateExtMethod:) - m4(1, separateExtMethod: 2.5) + _ = m4(1, separateExtMethod: 2.5) } func dynamicLookupMethod(_ b: AnyObject) { if let m5 = b.method(_:separateExtMethod:) { - m5(1, separateExtMethod: 2.5) + _ = m5(1, separateExtMethod: 2.5) } } @@ -204,15 +204,16 @@ func testProtocolMethods(_ b: B, p2m: P2.Type) { // Imported constructor. var b2 = B(viaP2: 3.14159, second: 3.14159) - p2m.init(viaP2:3.14159, second: 3.14159) + _ = p2m.init(viaP2:3.14159, second: 3.14159) } func testId(_ x: AnyObject) { x.perform!("foo:", with: x) // expected-warning{{no method declared with Objective-C selector 'foo:'}} + // expected-warning @-1 {{result of call is unused, but produces 'Unmanaged!'}} - x.performAdd(1, withValue: 2, withValue: 3, withValue2: 4) - x.performAdd!(1, withValue: 2, withValue: 3, withValue2: 4) - x.performAdd?(1, withValue: 2, withValue: 3, withValue2: 4) + _ = x.performAdd(1, withValue: 2, withValue: 3, withValue2: 4) + _ = x.performAdd!(1, withValue: 2, withValue: 3, withValue2: 4) + _ = x.performAdd?(1, withValue: 2, withValue: 3, withValue2: 4) } class MySubclass : B { @@ -224,7 +225,7 @@ class MySubclass : B { } func getDescription(_ array: NSArray) { - array.description + _ = array.description } // Method overriding with unfortunate ordering. @@ -257,7 +258,7 @@ func almostSubscriptableKeyMismatchInherited(_ roc: ReadOnlyCollectionChild, // Use of 'Class' via dynamic lookup. func classAnyObject(_ obj: NSObject) { - obj.myClass().description!() + _ = obj.myClass().description!() } // Protocol conformances @@ -307,8 +308,8 @@ func customAccessors(_ hive: Hive, bee: Bee) { markUsed(hive.makingHoney()) // expected-error{{cannot call value of non-function type 'Bool'}} hive.setMakingHoney(true) // expected-error{{value of type 'Hive' has no member 'setMakingHoney'}} - hive.`guard`.description // okay - hive.`guard`.description! // no-warning + _ = hive.`guard`.description // okay + _ = hive.`guard`.description! // no-warning hive.`guard` = bee // no-warning } @@ -339,7 +340,7 @@ func testDynamicSelf(_ queen: Bee, wobbler: NSWobbling) { } func testRepeatedProtocolAdoption(_ w: NSWindow) { - w.description + _ = w.description } class ProtocolAdopter1 : FooProto { diff --git a/test/Constraints/construction.swift b/test/Constraints/construction.swift index 046036d935ae7..e98b1459dc100 100644 --- a/test/Constraints/construction.swift +++ b/test/Constraints/construction.swift @@ -41,7 +41,7 @@ var z : Z = .none func acceptZ(_ z: Z) {} func acceptString(_ s: String) {} -Point(1, 2) +Point(1, 2) // expected-warning {{expression of type '(x: Int, y: Int)' is unused}} var db : Base = d X(i: 1, j: 2) // expected-warning{{unused}} Y(1, 2, "hello") // expected-warning{{unused}} @@ -60,7 +60,7 @@ _ = .none as Optional Optional(.none) // expected-error{{generic parameter 'T' could not be inferred}} // Interpolation -"\(hello), \(world) #\(i)!" +_ = "\(hello), \(world) #\(i)!" class File { init() { @@ -86,13 +86,13 @@ extension Foo { // Downcasting var b : Base -b as! Derived +_ = b as! Derived // Construction doesn't permit conversion. // NOTE: Int and other integer-literal convertible types // are special cased in the library. Int(i) // expected-warning{{unused}} -i as Int +_ = i as Int Z(z) // expected-error{{cannot invoke initializer for type 'Z' with an argument list of type '(Z)'}} // expected-note @-1 {{overloads for 'Z' exist with these partially matching parameter lists: (UnicodeScalar), (String)}} @@ -100,7 +100,7 @@ Z.init(z) // expected-error {{cannot invoke 'Z.Type.init' with an argument list // expected-note @-1 {{overloads for 'Z.Type.init' exist with these partially matching parameter lists: (UnicodeScalar), (String)}} -z as Z +_ = z as Z // Construction from inouts. struct FooRef { } diff --git a/test/Constraints/diagnostics.swift b/test/Constraints/diagnostics.swift index dca2351e11ccb..4d2e8a1933466 100644 --- a/test/Constraints/diagnostics.swift +++ b/test/Constraints/diagnostics.swift @@ -353,7 +353,7 @@ f7(1)(1.0) // expected-error {{missing argument label 'b:' in call}} f7(1)(b: 1.0) // expected-error {{cannot convert value of type 'Double' to expected argument type 'Int'}} let f8 = f7(2) -f8(b: 1) +_ = f8(b: 1) f8(10) // expected-error {{missing argument label 'b:' in call}} {{4-4=b: }} f8(1.0) // expected-error {{missing argument label 'b:' in call}} f8(b: 1.0) // expected-error {{cannot convert value of type 'Double' to expected argument type 'Int'}} diff --git a/test/Constraints/dynamic_lookup.swift b/test/Constraints/dynamic_lookup.swift index 3348ef177c5d6..2614262e08565 100644 --- a/test/Constraints/dynamic_lookup.swift +++ b/test/Constraints/dynamic_lookup.swift @@ -117,7 +117,7 @@ obj.foo!(5) obj.foo!("hello") obj.wibble!() obj.wobble!() // expected-error{{value of type 'Id' (aka 'AnyObject') has no member 'wobble'}} -obj.ext1!() +obj.ext1!() // expected-warning {{result of call is unused}} obj.wonka!() // Same as above but without the '!' @@ -126,7 +126,7 @@ obj.foo(5) obj.foo("hello") obj.wibble() obj.wobble() // expected-error{{value of type 'Id' (aka 'AnyObject') has no member 'wobble'}} -obj.ext1() +obj.ext1() // expected-warning {{result of call is unused}} obj.wonka() // Find class methods via dynamic method lookup. @@ -144,7 +144,7 @@ var ovl1Result = obj.ovl1!() ovl1Result = A() // verify that we got an A, not a B // Same as above but without the '!' -obj.ovl1() +obj.ovl1() // expected-warning {{result of call is unused}} // Don't allow overload resolution between declarations from different // classes. @@ -163,7 +163,7 @@ var ovl4Result = obj.ovl4!() var ovl5Result = obj.ovl5!() // expected-error{{ambiguous use of 'ovl5()'}} // Same as above but without the '!' -obj.ovl4() +obj.ovl4() // expected-warning {{result of call is unused}} // Generics diff --git a/test/Constraints/existential_metatypes.swift b/test/Constraints/existential_metatypes.swift index 16da85c0ffb92..6700a467e648c 100644 --- a/test/Constraints/existential_metatypes.swift +++ b/test/Constraints/existential_metatypes.swift @@ -69,7 +69,7 @@ extension P2 { } func testP2(_ pt: P2.Type) { - pt.init().elements + pt.init().elements // expected-warning {{expression of type '[P2]' is unused}} } // rdar://problem/21597711 diff --git a/test/Constraints/keyword_arguments.swift b/test/Constraints/keyword_arguments.swift index e28d401b086dd..0c8f3c4073d58 100644 --- a/test/Constraints/keyword_arguments.swift +++ b/test/Constraints/keyword_arguments.swift @@ -282,13 +282,13 @@ func intToInt(_ i: Int) -> Int { return i } func testClosures() { let c0 = { (x: Int, y: Int) in x + y } - c0(1, 2) + _ = c0(1, 2) let c1 = { x, y in intToInt(x + y) } - c1(1, 2) + _ = c1(1, 2) let c2 = { intToInt($0 + $1) } - c2(1, 2) + _ = c2(1, 2) } func acceptAutoclosure(f: @autoclosure () -> Int) { } diff --git a/test/Constraints/members.swift b/test/Constraints/members.swift index d8e4232562d36..f6a1c7f3dfe23 100644 --- a/test/Constraints/members.swift +++ b/test/Constraints/members.swift @@ -147,7 +147,7 @@ func goo() { func id(_ t: T) -> T { return t } func doGetLogicValue(_ t: T) { - t.boolValue + t.boolValue // expected-warning {{expression of type 'Bool' is unused}} } protocol P { diff --git a/test/Constraints/members_objc.swift b/test/Constraints/members_objc.swift index 9b30518086ff9..2cc9b15f99a5b 100644 --- a/test/Constraints/members_objc.swift +++ b/test/Constraints/members_objc.swift @@ -30,5 +30,5 @@ func archetype(_ p2 : T) { func test_subject_ClassConstrainedSubscript() { let list: subject_ClassConstrainedSubscript! = test_HasSubscript() - list[0] + _ = list[0] } diff --git a/test/Misc/single_expr_closure_conversion.swift b/test/Misc/single_expr_closure_conversion.swift index 258e665c2ff81..4d9cc310f7dc1 100644 --- a/test/Misc/single_expr_closure_conversion.swift +++ b/test/Misc/single_expr_closure_conversion.swift @@ -45,6 +45,8 @@ class C { var a = A() func act() { - a.dispatch({() -> Void in self.prop}) + a.dispatch({() -> Void in + self.prop // expected-warning {{expression of type 'Int' is unused}} + }) } } diff --git a/test/NameBinding/name-binding.swift b/test/NameBinding/name-binding.swift index 0f2cea193b74c..72d421171ece8 100644 --- a/test/NameBinding/name-binding.swift +++ b/test/NameBinding/name-binding.swift @@ -225,7 +225,7 @@ struct Matrix4 { func r19558785() { let b = 10 for b in 0...b { - b + _ = b } } diff --git a/test/Parse/EOF/interpolated-string-literal.swift b/test/Parse/EOF/interpolated-string-literal.swift index e654acd847edc..caaf0a8b6ecc6 100644 --- a/test/Parse/EOF/interpolated-string-literal.swift +++ b/test/Parse/EOF/interpolated-string-literal.swift @@ -3,4 +3,4 @@ // Parser used to crash while parsing a file where the last token was an // interpolated string literal. -"\(1)" +_ = "\(1)" diff --git a/test/Parse/comment_operator.swift b/test/Parse/comment_operator.swift index f46ce7cb0c5b2..05a3cc4b3e603 100644 --- a/test/Parse/comment_operator.swift +++ b/test/Parse/comment_operator.swift @@ -24,6 +24,6 @@ func test5() { _ = !/* */foo } // expected-error {{unary operator func test6() { _ = 1+/* */2 } // expected-error {{'+' is not a postfix unary operator}} expected-error {{consecutive statements on a line must be separated by ';'}} expected-warning {{result of call to 'init(_builtinIntegerLiteral:)' is unused}} // Continue to work -foo!// this is dangerous +_ = foo!// this is dangerous _ = 1 +/**/ 2 _ = 1 +/* hi */2 diff --git a/test/Parse/consecutive_statements.swift b/test/Parse/consecutive_statements.swift index 5c9cf3ef279c1..9fc4167e4573d 100644 --- a/test/Parse/consecutive_statements.swift +++ b/test/Parse/consecutive_statements.swift @@ -13,7 +13,7 @@ func statement_starts() { a[0] = 1 a [0] = 1 a // expected-error{{expression resolves to an unused l-value}} - [0, 1, 2] + [0, 1, 2] // expected-warning {{expression of type '[Int]' is unused}} } // Within a function diff --git a/test/Parse/generic_disambiguation.swift b/test/Parse/generic_disambiguation.swift index 0d43437937e7c..63c58cd6c225b 100644 --- a/test/Parse/generic_disambiguation.swift +++ b/test/Parse/generic_disambiguation.swift @@ -24,7 +24,7 @@ func generic(_ x: T) {} var a, b, c, d : Int _ = a < b -(a < b, c > d) +_ = (a < b, c > d) // Parses as generic because of lparen after '>' (a < b, c > (d)) // expected-error{{use of undeclared type 'b'}} // Parses as generic because of lparen after '>' @@ -67,6 +67,6 @@ meta2(A.C.self, 0) A.c() // expected-error{{generic type 'A' specialized with too many type parameters (got 2, but expected 1)}} A(x: 0) // parses as type // expected-warning{{unused}} -a < b ? c : d +_ = a < b ? c : d A<(B) throws -> D>(x: 0) // expected-warning{{unused}} diff --git a/test/Parse/hashbang_main.swift b/test/Parse/hashbang_main.swift index ee4315a8e5c8b..804d5b839a34d 100644 --- a/test/Parse/hashbang_main.swift +++ b/test/Parse/hashbang_main.swift @@ -1,6 +1,6 @@ #!/usr/bin/swift let x = 42 -x + x // expected-warning {{result of call to '+' is unused}} +x + x // expected-warning {{result of operator '+' is unused}} // Check that we skip the hashbang at the beginning of the file. // RUN: %target-parse-verify-swift diff --git a/test/Parse/line-directive.swift b/test/Parse/line-directive.swift index f4febffc991a6..9cc3fe36896d7 100644 --- a/test/Parse/line-directive.swift +++ b/test/Parse/line-directive.swift @@ -17,8 +17,9 @@ x // expected-error {{parameterless closing #sourceLocation() directive without x x ; // should be ignored by expected_error because it is in a different file x #sourceLocation() -x +_ = x x x // expected-error{{consecutive statements}} {{2-2=;}} +// expected-warning @-1 2 {{unused}} // rdar://19582475 public struct S { // expected-note{{in declaration of 'S'}} diff --git a/test/Parse/operators.swift b/test/Parse/operators.swift index 6039c508354d9..4fe6c3faee6e8 100644 --- a/test/Parse/operators.swift +++ b/test/Parse/operators.swift @@ -109,5 +109,7 @@ func !!(x: Man, y: Man) {} let foo = Man() let bar = TheDevil() foo!!foo // expected-error{{cannot force unwrap value of non-optional type 'Man'}} {{4-5=}} expected-error{{consecutive statements}} {{6-6=;}} -foo??bar // expected-error{{broken standard library}} expected-error{{consecutive statements}} {{6-6=;}} +// expected-warning @-1 {{expression of type 'Man' is unused}} +foo??bar // expected-error{{broken standard library}} expected-error{{consecutive statements}} {{6-6=;}} +// expected-warning @-1 {{expression of type 'TheDevil' is unused}} diff --git a/test/Parse/recovery.swift b/test/Parse/recovery.swift index af0af86a803ea..6fa8f8a4e7ba9 100644 --- a/test/Parse/recovery.swift +++ b/test/Parse/recovery.swift @@ -128,7 +128,7 @@ func missingControllingExprInRepeatWhile() { } repeat { - } while { true }() // expected-error{{missing condition in a 'while' statement}} expected-error{{consecutive statements on a line must be separated by ';'}} {{10-10=;}} + } while { true }() // expected-error{{missing condition in a 'while' statement}} expected-error{{consecutive statements on a line must be separated by ';'}} {{10-10=;}} expected-warning {{result of call is unused, but produces 'Bool'}} } // SR-165 @@ -405,6 +405,7 @@ struct MissingInitializer1 { func exprPostfix1(x : Int) { x. // expected-error {{expected member name following '.'}} + // expected-warning @-1 {{expression of type 'Int' is unused}} } func exprPostfix2() { @@ -623,6 +624,7 @@ func test23086402(a: A23086402) { func test23719432() { var x = 42 &(Int:x) // expected-error {{'&' can only appear immediately in a call argument list}} + // expected-warning @-1 {{expression of type 'inout (Int: Int)' is unused}} } // QoI: terrible recovery when using 'ยท' for an operator @@ -644,6 +646,7 @@ func postfixDot(a : String) { _ = a. utf8 // expected-error {{extraneous whitespace after '.' is not permitted}} {{9-12=}} _ = a. // expected-error {{expected member name following '.'}} a. // expected-error {{expected member name following '.'}} + // expected-warning @-1 {{expression of type 'String' is unused}} } // QoI: Invalid trailing closures in stmt-conditions produce lowsy diagnostics diff --git a/test/Parse/toplevel_library_invalid.swift b/test/Parse/toplevel_library_invalid.swift index d203f0e39589a..7eaccf08544d9 100644 --- a/test/Parse/toplevel_library_invalid.swift +++ b/test/Parse/toplevel_library_invalid.swift @@ -1,11 +1,13 @@ // RUN: %target-parse-verify-swift -parse-as-library let x = 42 -x + x; // expected-error {{expressions are not allowed at the top level}} expected-warning {{result of call to '+' is unused}} -x + x; // expected-error {{expressions are not allowed at the top level}} expected-warning {{result of call to '+' is unused}} +x + x; // expected-error {{expressions are not allowed at the top level}} expected-warning {{result of operator '+' is unused}} +x + x; // expected-error {{expressions are not allowed at the top level}} expected-warning {{result of operator '+' is unused}} // Make sure we don't crash on closures at the top level ({ }) // expected-error {{expressions are not allowed at the top level}} expected-error{{expression resolves to an unused function}} ({ 5 }()) // expected-error {{expressions are not allowed at the top level}} +// expected-warning @-1 {{result of call is unused}} + // FIXME: Too many errors for this. for i // expected-error 2 {{expected ';' in 'for' statement}} diff --git a/test/Parse/try.swift b/test/Parse/try.swift index 0fd62d3bad609..6d78ac5e6d614 100644 --- a/test/Parse/try.swift +++ b/test/Parse/try.swift @@ -14,7 +14,7 @@ func bar() throws -> Int { return 0 } var x = try foo() + bar() x = try foo() + bar() x += try foo() + bar() -x += try foo() %%%% bar() // expected-error {{'try' following assignment operator does not cover everything to its right}} // expected-error {{call can throw but is not marked with 'try'}} // expected-warning {{result of call to '%%%%' is unused}} +x += try foo() %%%% bar() // expected-error {{'try' following assignment operator does not cover everything to its right}} // expected-error {{call can throw but is not marked with 'try'}} // expected-warning {{result of operator '%%%%' is unused}} x += try foo() %%% bar() x = foo() + try bar() // expected-error {{'try' cannot appear to the right of a non-assignment operator}} // expected-error {{call can throw but is not marked with 'try'}} @@ -24,7 +24,7 @@ var z = true ? try foo() : try bar() %%% 0 // expected-error {{'try' following c var a = try! foo() + bar() a = try! foo() + bar() a += try! foo() + bar() -a += try! foo() %%%% bar() // expected-error {{'try!' following assignment operator does not cover everything to its right}} // expected-error {{call can throw but is not marked with 'try'}} // expected-warning {{result of call to '%%%%' is unused}} +a += try! foo() %%%% bar() // expected-error {{'try!' following assignment operator does not cover everything to its right}} // expected-error {{call can throw but is not marked with 'try'}} // expected-warning {{result of operator '%%%%' is unused}} a += try! foo() %%% bar() a = foo() + try! bar() // expected-error {{'try!' cannot appear to the right of a non-assignment operator}} // expected-error {{call can throw but is not marked with 'try'}} @@ -40,7 +40,7 @@ var i = try? foo() + bar() let _: Double = i // expected-error {{cannot convert value of type 'Int?' to specified type 'Double'}} i = try? foo() + bar() i ?+= try? foo() + bar() -i ?+= try? foo() %%%% bar() // expected-error {{'try?' following assignment operator does not cover everything to its right}} // expected-error {{call can throw but is not marked with 'try'}} // expected-warning {{result of call to '%%%%' is unused}} +i ?+= try? foo() %%%% bar() // expected-error {{'try?' following assignment operator does not cover everything to its right}} // expected-error {{call can throw but is not marked with 'try'}} // expected-warning {{result of operator '%%%%' is unused}} i ?+= try? foo() %%% bar() _ = foo() < try? bar() // expected-error {{'try?' cannot appear to the right of a non-assignment operator}} // expected-error {{call can throw but is not marked with 'try'}} _ = (try? foo()) < bar() // expected-error {{call can throw but is not marked with 'try'}} @@ -100,7 +100,7 @@ struct r21432429 { // Swift 2: Omitting try from call to throwing closure in rethrowing function crashes compiler func callThrowingClosureWithoutTry(closure: (Int) throws -> Int) rethrows { - closure(0) // expected-error {{call can throw but is not marked with 'try'}} + closure(0) // expected-error {{call can throw but is not marked with 'try'}} expected-warning {{result of call is unused}} } func producesOptional() throws -> Int? { return nil } diff --git a/test/SILGen/objc_blocks_bridging.swift b/test/SILGen/objc_blocks_bridging.swift index 7333f89979234..5a541a9c2b3e5 100644 --- a/test/SILGen/objc_blocks_bridging.swift +++ b/test/SILGen/objc_blocks_bridging.swift @@ -42,7 +42,7 @@ import Foundation // CHECK: [[NATIVE:%.*]] = function_ref @_TFC20objc_blocks_bridging3Foo16cFunctionPointer // CHECK: apply [[NATIVE]]([[F]], [[X]], [[SELF]]) dynamic func cFunctionPointer(_ fp: @convention(c) (Int) -> Int, x: Int) -> Int { - fp(x) + _ = fp(x) } // Blocks and C function pointers must not be reabstracted when placed in optionals. diff --git a/test/SILOptimizer/definite_init_diagnostics.swift b/test/SILOptimizer/definite_init_diagnostics.swift index 43f86999bc175..dc250582cd518 100644 --- a/test/SILOptimizer/definite_init_diagnostics.swift +++ b/test/SILOptimizer/definite_init_diagnostics.swift @@ -47,6 +47,7 @@ func test2() { // Address-of with Builtin.addressof. var a4 : Int // expected-note {{variable defined here}} Builtin.addressof(&a4) // expected-error {{address of variable 'a4' taken before it is initialized}} + // expected-warning @-1 {{result of call is unused, but produces 'Builtin.RawPointer'}} // Closures. diff --git a/test/Sema/availability_versions.swift b/test/Sema/availability_versions.swift index d5e772eacebfd..1996e27702bb9 100644 --- a/test/Sema/availability_versions.swift +++ b/test/Sema/availability_versions.swift @@ -634,8 +634,8 @@ func classAvailability() { // expected-note@-1 {{add @available attribute to enclosing global function}} // expected-note@-2 {{add 'if #available' version check}} - ClassAvailableOn10_9.self - ClassAvailableOn10_51.self // expected-error {{'ClassAvailableOn10_51' is only available on OS X 10.51 or newer}} + _ = ClassAvailableOn10_9.self + _ = ClassAvailableOn10_51.self // expected-error {{'ClassAvailableOn10_51' is only available on OS X 10.51 or newer}} // expected-note@-1 {{add @available attribute to enclosing global function}} // expected-note@-2 {{add 'if #available' version check}} diff --git a/test/Sema/diag_values_of_module_type.swift b/test/Sema/diag_values_of_module_type.swift index 109f9f70ab754..fe2c8d98d01ba 100644 --- a/test/Sema/diag_values_of_module_type.swift +++ b/test/Sema/diag_values_of_module_type.swift @@ -98,10 +98,10 @@ func badTest3() { var _ = Swift. // expected-error {{expected member name following '.'}} expected-error {{expected module member name after module name}} } func badTest4() { - Swift // expected-error {{expected module member name after module name}} + _ = Swift // expected-error {{expected module member name after module name}} } func badTest5() { - Swift. // expected-error {{expected module member name after module name}} expected-error {{expected member name following '.'}} + _ = Swift. // expected-error {{expected module member name after module name}} expected-error {{expected member name following '.'}} } func badTest6() { _ = { () -> Int in diff --git a/test/Sema/editor_placeholders.swift b/test/Sema/editor_placeholders.swift index d6e2b4105f325..a794a17ac303b 100644 --- a/test/Sema/editor_placeholders.swift +++ b/test/Sema/editor_placeholders.swift @@ -20,7 +20,7 @@ f(a2) <#T#> // expected-error{{editor placeholder in source file}} // FIXME: Lexer yields "editor placeholder in source file" error twice when placeholder is first token -<#T##Int#> // expected-error 2{{editor placeholder in source file}} +_ = <#T##Int#> // expected-error {{editor placeholder in source file}} f(<#T#> + 1) // expected-error{{editor placeholder in source file}} f(<#T##Int#>) // expected-error{{editor placeholder in source file}} diff --git a/test/attr/attr_discardableResult.swift b/test/attr/attr_discardableResult.swift index 78726704b22f2..4bf4f20c95c1b 100644 --- a/test/attr/attr_discardableResult.swift +++ b/test/attr/attr_discardableResult.swift @@ -52,9 +52,9 @@ func testFunctionsInClass(c1 : C1) { C1.f2Class() // expected-warning {{result of call to 'f2Class()' is unused}} _ = C1.f2Class() // okay - C1() // expected-warning {{result of initializer is unused}} + C1() // okay, marked @discardableResult _ = C1() // okay - C1(foo: 5) // expected-warning {{result of call to 'init(foo:)' is unused}} + C1(foo: 5) // expected-warning {{result of 'C1' initializer is unused}} _ = C1(foo: 5) // okay c1.f1() // okay @@ -82,12 +82,27 @@ func testFunctionsInStruct(s1 : S1) { S1.f2Static() // expected-warning {{result of call to 'f2Static()' is unused}} _ = S1.f2Static() // okay - S1() // expected-warning {{result of initializer is unused}} + S1() // okay, marked @discardableResult _ = S1() // okay - S1(foo: 5) // expected-warning {{result of call to 'init(foo:)' is unused}} + S1(foo: 5) // expected-warning {{result of 'S1' initializer is unused}} _ = S1(foo: 5) // okay s1.f1() // okay s1.f2() // expected-warning {{result of call to 'f2()' is unused}} _ = s1.f2() // okay } + + +let x = 4 +"Hello \(x+1) world" // expected-warning {{expression of type 'String' is unused}} + +func f(a : () -> Int) { + 42 // expected-warning {{result of call to 'init(_builtinIntegerLiteral:)' is unused}} + + 4 + 5 // expected-warning {{result of operator '+' is unused}} + a() // expected-warning {{result of call is unused, but produces 'Int'}} +} + + + + diff --git a/test/attr/attr_noescape.swift b/test/attr/attr_noescape.swift index 4b2c5233118c9..1ef7949bb8d0d 100644 --- a/test/attr/attr_noescape.swift +++ b/test/attr/attr_noescape.swift @@ -10,7 +10,7 @@ func takesGenericClosure(_ a : Int, _ fn : @noescape () -> T) {} func takesNoEscapeClosure(_ fn : @noescape () -> Int) { takesNoEscapeClosure { 4 } // ok - fn() // ok + _ = fn() // ok var x = fn // expected-error {{@noescape parameter 'fn' may only be called}} @@ -22,7 +22,7 @@ func takesNoEscapeClosure(_ fn : @noescape () -> Int) { // This is not ok, because it escapes the 'fn' closure. func nested_function() { - fn() // expected-error {{declaration closing over @noescape parameter 'fn' may allow it to escape}} + _ = fn() // expected-error {{declaration closing over @noescape parameter 'fn' may allow it to escape}} } takesNoEscapeClosure(fn) // ok diff --git a/test/decl/enum/enumtest.swift b/test/decl/enum/enumtest.swift index 71040e7822cae..b6c0de76a2cff 100644 --- a/test/decl/enum/enumtest.swift +++ b/test/decl/enum/enumtest.swift @@ -75,10 +75,12 @@ func test3(_ a: ZeroOneTwoThree) { var _ : Int = ZeroOneTwoThree.Zero // expected-error {{cannot convert value of type 'ZeroOneTwoThree' to specified type 'Int'}} + // expected-warning @+1 {{unused}} test3 ZeroOneTwoThree.Zero // expected-error {{expression resolves to an unused function}} expected-error{{consecutive statements}} {{8-8=;}} test3 (ZeroOneTwoThree.Zero) test3(ZeroOneTwoThree.Zero) test3 // expected-error {{expression resolves to an unused function}} + // expected-warning @+1 {{unused}} (ZeroOneTwoThree.Zero) var _ : ZeroOneTwoThree = .One(4) diff --git a/test/decl/ext/generic.swift b/test/decl/ext/generic.swift index f07e056c79465..1e4bac8e4fc65 100644 --- a/test/decl/ext/generic.swift +++ b/test/decl/ext/generic.swift @@ -116,10 +116,12 @@ func notHashableArray(_ x: [T]) { } func hashableArray(_ x: [T]) { + // expected-warning @+1 {{unused}} x.worseHashEver // okay } func intArray(_ x: [Int]) { + // expected-warning @+1 {{unused}} x.worseHashEver } diff --git a/test/decl/ext/protocol_objc.swift b/test/decl/ext/protocol_objc.swift index 61c80f578de0f..9b846620841e4 100644 --- a/test/decl/ext/protocol_objc.swift +++ b/test/decl/ext/protocol_objc.swift @@ -16,6 +16,7 @@ class OC1 : OP1 { func testOP1(_ oc1: OC1, ao: AnyObject) { _ = oc1.extOP1a() + // expected-warning @+1 {{result of call is unused}} ao.reqOP1a!() // okay // Extension of @objc protocol does not have @objc members. diff --git a/test/decl/func/functions.swift b/test/decl/func/functions.swift index cb53e2b6229cc..5109222e81eca 100644 --- a/test/decl/func/functions.swift +++ b/test/decl/func/functions.swift @@ -93,7 +93,7 @@ func parenPatternInArg((a): Int) -> Int { // expected-error {{expected parameter parenPatternInArg(0) // expected-error {{argument passed to call that takes no arguments}} var nullaryClosure: (Int) -> Int = {_ in 0} -nullaryClosure(0) +_ = nullaryClosure(0) // rdar://16737322 - This argument is an unnamed argument that has a labeled diff --git a/test/decl/func/throwing_functions.swift b/test/decl/func/throwing_functions.swift index 13726805792eb..3aa566f5a00c9 100644 --- a/test/decl/func/throwing_functions.swift +++ b/test/decl/func/throwing_functions.swift @@ -115,9 +115,9 @@ var c2 : () throws -> Int = c1 // ok var c3 : () -> Int = c1 // expected-error{{invalid conversion from throwing function of type '() throws -> Int' to non-throwing function type '() -> Int'}} var c4 : () -> Int = {() throws -> Int in 0} // expected-error{{invalid conversion from throwing function of type '() throws -> Int' to non-throwing function type '() -> Int'}} var c5 : () -> Int = { try c2() } // expected-error{{invalid conversion from throwing function of type '() throws -> Int' to non-throwing function type '() -> Int'}} -var c6 : () throws -> Int = { do { try c2() } ; return 0 } +var c6 : () throws -> Int = { do { _ = try c2() } ; return 0 } var c7 : () -> Int = { do { try c2() } ; return 0 } // expected-error{{invalid conversion from throwing function of type '() throws -> _' to non-throwing function type '() -> Int'}} -var c8 : () -> Int = { do { try c2() } catch _ { var x = 0 } ; return 0 } +var c8 : () -> Int = { do { _ = try c2() } catch _ { var x = 0 } ; return 0 } var c9 : () -> Int = { do { try c2() } catch Exception.A { var x = 0 } ; return 0 }// expected-error{{invalid conversion from throwing function of type '() throws -> _' to non-throwing function type '() -> Int'}} var c10 : () -> Int = { throw Exception.A; return 0 } // expected-error{{invalid conversion from throwing function of type '() throws -> _' to non-throwing function type '() -> Int'}} var c11 : () -> Int = { try! c2() } diff --git a/test/decl/protocol/protocols.swift b/test/decl/protocol/protocols.swift index cd936639c7a6f..4789cee748e37 100644 --- a/test/decl/protocol/protocols.swift +++ b/test/decl/protocol/protocols.swift @@ -453,7 +453,7 @@ protocol SecondProtocol { // Can't upcast to parent types of type constraints without forcing class C1 : P2 {} func f(_ x : T) { - x as P2 + _ = x as P2 } class C2 {} @@ -463,7 +463,7 @@ func g(_ x : T) { class C3 : P1 {} // expected-error{{type 'C3' does not conform to protocol 'P1'}} func h(_ x : T) { - x as P1 // expected-error{{protocol 'P1' can only be used as a generic constraint because it has Self or associated type requirements}} + _ = x as P1 // expected-error{{protocol 'P1' can only be used as a generic constraint because it has Self or associated type requirements}} } diff --git a/test/expr/cast/array_iteration.swift b/test/expr/cast/array_iteration.swift index ac871fc5df161..3802b8c34185b 100644 --- a/test/expr/cast/array_iteration.swift +++ b/test/expr/cast/array_iteration.swift @@ -10,7 +10,7 @@ var rootView = View() var v = [View(), View()] rootView.subviews = v -rootView.subviews as! [View] +_ = rootView.subviews as! [View] for view in rootView.subviews as! [View] { doFoo() @@ -22,12 +22,12 @@ for view:View in rootView.subviews { // expected-error{{type 'Array!' doFoo() } -(rootView.subviews!) as! [View] +_ = (rootView.subviews!) as! [View] -(rootView.subviews) as! [View] +_ = (rootView.subviews) as! [View] var ao: [AnyObject] = [] -ao as! [View] // works +_ = ao as! [View] // works var b = Array<(String, Int)>() diff --git a/test/expr/cast/as_coerce.swift b/test/expr/cast/as_coerce.swift index ec5b4f4ba0aaa..5ca0fdb0115e9 100644 --- a/test/expr/cast/as_coerce.swift +++ b/test/expr/cast/as_coerce.swift @@ -59,7 +59,7 @@ if let p = cc as? P { // Test that 'as?' coercion fails. let strImplicitOpt: String! = nil -strImplicitOpt as? String // expected-warning{{conditional cast from 'String!' to 'String' always succeeds}} +_ = strImplicitOpt as? String // expected-warning{{conditional cast from 'String!' to 'String' always succeeds}} class C3 {} class C4 : C3 {} @@ -92,7 +92,7 @@ Double(1) as Double as String // expected-error{{cannot convert value of type 'D [(1, (1, 1))] as! [(Int, (String, Int))] // expected-error{{'[(Int, (Int, Int))]' is not convertible to '[(Int, (String, Int))]'}} // Incorrect diagnostic for explicitly casting to the same type -"hello" as! String // expected-warning{{forced cast of 'String' to same type has no effect}} {{9-20=}} +_ = "hello" as! String // expected-warning{{forced cast of 'String' to same type has no effect}} {{13-24=}} // QoI: Nimble as -> as! changes not covered by Fix-Its func f(_ x : String) {} diff --git a/test/expr/cast/dictionary_bridge.swift b/test/expr/cast/dictionary_bridge.swift index 8c6543bf4e673..a4b4dd5a9ddba 100644 --- a/test/expr/cast/dictionary_bridge.swift +++ b/test/expr/cast/dictionary_bridge.swift @@ -111,19 +111,19 @@ func testDowncastBridge() { let dictOB = Dictionary() // Downcast to bridged value types. - dictRR as! Dictionary - dictRR as! Dictionary - dictRR as! Dictionary + _ = dictRR as! Dictionary + _ = dictRR as! Dictionary + _ = dictRR as! Dictionary - dictRO as! Dictionary - dictRO as! Dictionary - dictRO as! Dictionary + _ = dictRO as! Dictionary + _ = dictRO as! Dictionary + _ = dictRO as! Dictionary - dictBO as! Dictionary - dictOB as! Dictionary + _ = dictBO as! Dictionary + _ = dictOB as! Dictionary // We don't do mixed down/upcasts. - dictDO as! Dictionary // expected-error{{'Dictionary' is not convertible to 'Dictionary'}} + _ = dictDO as! Dictionary // expected-error{{'Dictionary' is not convertible to 'Dictionary'}} } func testConditionalDowncastBridge() { diff --git a/test/expr/cast/set_bridge.swift b/test/expr/cast/set_bridge.swift index eeb9e824fe218..c569915e37f62 100644 --- a/test/expr/cast/set_bridge.swift +++ b/test/expr/cast/set_bridge.swift @@ -77,18 +77,18 @@ func testForcedDowncastBridge() { let setD = Set() let setB = Set() - setR as! Set - setO as! Set - setD as! Set // expected-error {{'ObjC' is not a subtype of 'DerivesObjC'}} + _ = setR as! Set + _ = setO as! Set + _ = setD as! Set // expected-error {{'ObjC' is not a subtype of 'DerivesObjC'}} // expected-note @-1 {{in cast from type 'Set' to 'Set'}} // TODO: the diagnostic for the below two examples should indicate that 'as' // should be used instead of 'as!' - setB as! Set // expected-error {{'Root' is not a subtype of 'BridgedToObjC'}} + _ = setB as! Set // expected-error {{'Root' is not a subtype of 'BridgedToObjC'}} // expected-note @-1 {{in cast from type 'Set' to 'Set'}} - setB as! Set // expected-error {{'ObjC' is not a subtype of 'BridgedToObjC'}} + _ = setB as! Set // expected-error {{'ObjC' is not a subtype of 'BridgedToObjC'}} // expected-note @-1 {{in cast from type 'Set' to 'Set'}} - setB as! Set // expected-error {{'DerivesObjC' is not a subtype of 'BridgedToObjC'}} + _ = setB as! Set // expected-error {{'DerivesObjC' is not a subtype of 'BridgedToObjC'}} // expected-note @-1 {{in cast from type 'Set' to 'Set'}} } diff --git a/test/expr/closure/basic.swift b/test/expr/closure/basic.swift index 782b6b0f78224..342f18ed49c9d 100644 --- a/test/expr/closure/basic.swift +++ b/test/expr/closure/basic.swift @@ -22,9 +22,9 @@ func variadic() { } return result } - f(1) - f(1, 2) - f(1, 3) + _ = f(1) + _ = f(1, 2) + _ = f(1, 3) let D = { (Ss ...) in 1 } // expected-error{{'...' cannot be applied to a subpattern which is not explicitly typed}}, expected-error{{unable to infer closure type in the current context}} } @@ -38,6 +38,6 @@ func attrs() { func argAndParamNames() -> Int { let _: (x: Int, y: Int) -> Int = { (a x, b y) in x + y } // expected-error 2 {{closure cannot have keyword arguments}} let f1: (x: Int, y: Int) -> Int = { (x, y) in x + y } - f1(x: 1, y: 2) + _ = f1(x: 1, y: 2) return f1(x: 1, y: 2) } diff --git a/test/expr/closure/closures.swift b/test/expr/closure/closures.swift index cd2097020dd76..5b62ae190cace 100644 --- a/test/expr/closure/closures.swift +++ b/test/expr/closure/closures.swift @@ -244,7 +244,9 @@ func rdar19179412() -> (Int) -> Int { func takesVoidFunc(_ f: () -> ()) {} var i: Int = 1 +// expected-warning @+1 {{expression of type 'Int' is unused}} takesVoidFunc({i}) +// expected-warning @+1 {{expression of type 'Int' is unused}} var f1: () -> () = {i} var x = {return $0}(1) diff --git a/test/expr/expressions.swift b/test/expr/expressions.swift index 451eb8d454bc8..6971312a10a85 100644 --- a/test/expr/expressions.swift +++ b/test/expr/expressions.swift @@ -229,6 +229,7 @@ func test_lambda() { } func test_lambda2() { + // expected-warning @+1 {{result of call is unused}} { () -> protocol in // expected-error {{non-protocol type 'Int' cannot be used within 'protocol<...>'}} return 1 }() @@ -406,7 +407,7 @@ func stringliterals(_ d: [String: Int]) { // rdar://11385385 let x = 4 - "Hello \(x+1) world" + "Hello \(x+1) world" // expected-warning {{expression of type 'String' is unused}} "Error: \(x+1"; // expected-error {{unterminated string literal}} @@ -610,7 +611,7 @@ func lvalue_processing() { var n = 42 fn(n, 12) // expected-error {{passing value of type 'Int' to an inout parameter requires explicit '&'}} {{6-6=&}} - fn(&n, 12) + fn(&n, 12) // expected-warning {{result of call is unused, but produces 'Int'}} n +-+= 12 @@ -691,11 +692,11 @@ nil != Int.self // expected-error {{binary operator '!=' cannot be applied to op // Disallow postfix ? when not chaining func testOptionalChaining(_ a : Int?, b : Int!, c : Int??) { - a? // expected-error {{optional chain has no effect, expression already produces 'Int?'}} {{4-5=}} - a?.customMirror + _ = a? // expected-error {{optional chain has no effect, expression already produces 'Int?'}} {{8-9=}} + _ = a?.customMirror - b? // expected-error {{'?' must be followed by a call, member lookup, or subscript}} - b?.customMirror + _ = b? // expected-error {{'?' must be followed by a call, member lookup, or subscript}} + _ = b?.customMirror var _: Int? = c? // expected-error {{'?' must be followed by a call, member lookup, or subscript}} } @@ -750,6 +751,7 @@ public struct TestPropMethodOverloadGroup { func inoutTests(_ arr: inout Int) { var x = 1, y = 2 (true ? &x : &y) // expected-error 2 {{'&' can only appear immediately in a call argument list}} + // expected-warning @-1 {{expression of type 'inout Int' is unused}} let a = (true ? &x : &y) // expected-error 2 {{'&' can only appear immediately in a call argument list}} // expected-error @-1 {{type 'inout Int' of variable is not materializable}} diff --git a/test/expr/postfix/dot/init_ref_delegation.swift b/test/expr/postfix/dot/init_ref_delegation.swift index e98b0afa0033f..67acdf7ccf211 100644 --- a/test/expr/postfix/dot/init_ref_delegation.swift +++ b/test/expr/postfix/dot/init_ref_delegation.swift @@ -151,7 +151,7 @@ struct RDar16603812 { init() {} func foo() { self.init() // expected-error {{'init' is a member of the type; insert '.dynamicType' to initialize a new object of the same dynamic type}} {{11-11=.dynamicType}} - self.dynamicType.init() // expected-warning{{result of call to 'init()' is unused}} + self.dynamicType.init() // expected-warning{{result of 'RDar16603812' initializer is unused}} } } @@ -343,7 +343,7 @@ class TestNestedExpr { } convenience init(c: Int) { - ((), self.init()) // expected-error {{initializer delegation ('self.init') cannot be nested in another expression}} + _ = ((), self.init()) // expected-error {{initializer delegation ('self.init') cannot be nested in another expression}} } convenience init(d: Int) { @@ -357,7 +357,7 @@ class TestNestedExpr { } convenience init(f: Int) { - ((), self.init(fail: true)!) // expected-error {{initializer delegation ('self.init') cannot be nested in another expression}} + _ = ((), self.init(fail: true)!) // expected-error {{initializer delegation ('self.init') cannot be nested in another expression}} } convenience init(g: Int) { @@ -371,7 +371,7 @@ class TestNestedExpr { } convenience init(i: Int) { - ((), try! self.init(error: true)) // expected-error {{initializer delegation ('self.init') cannot be nested in another expression}} + _ = ((), try! self.init(error: true)) // expected-error {{initializer delegation ('self.init') cannot be nested in another expression}} } } @@ -387,7 +387,7 @@ class TestNestedExprSub : TestNestedExpr { } init(c: Int) { - ((), super.init()) // expected-error {{initializer chaining ('super.init') cannot be nested in another expression}} + _ = ((), super.init()) // expected-error {{initializer chaining ('super.init') cannot be nested in another expression}} } init(d: Int) { @@ -401,7 +401,7 @@ class TestNestedExprSub : TestNestedExpr { } init(f: Int) { - ((), super.init(fail: true)!) // expected-error {{initializer chaining ('super.init') cannot be nested in another expression}} + _ = ((), super.init(fail: true)!) // expected-error {{initializer chaining ('super.init') cannot be nested in another expression}} } init(g: Int) { @@ -415,7 +415,7 @@ class TestNestedExprSub : TestNestedExpr { } init(i: Int) { - ((), try! super.init(error: true)) // expected-error {{initializer chaining ('super.init') cannot be nested in another expression}} + _ = ((), try! super.init(error: true)) // expected-error {{initializer chaining ('super.init') cannot be nested in another expression}} } } diff --git a/test/stmt/statements.swift b/test/stmt/statements.swift index f4cdaa48810a3..04cadf7e13a31 100644 --- a/test/stmt/statements.swift +++ b/test/stmt/statements.swift @@ -328,7 +328,7 @@ func testMyEnumWithCaseLabels(_ a : MyEnumWithCaseLabels) { func test_defer(_ a : Int) { defer { VoidReturn1() } - defer { breakContinue(1)+42 } // expected-warning {{result of call to '+' is unused}} + defer { breakContinue(1)+42 } // expected-warning {{result of operator '+' is unused}} // Ok: defer { while false { break } } diff --git a/validation-test/Sema/OverridesAndOverloads.swift b/validation-test/Sema/OverridesAndOverloads.swift index d1e0484acedc2..fdb8c9e42286f 100644 --- a/validation-test/Sema/OverridesAndOverloads.swift +++ b/validation-test/Sema/OverridesAndOverloads.swift @@ -122,10 +122,10 @@ Overrides.test("contravariant return type override, optional to non-optional") { } } - Derived().foo() as C1 + _ = Derived().foo() as C1 expectEqual("Derived.foo() -> C1", which) - Derived().foo() as C1? + _ = Derived().foo() as C1? expectEqual("Derived.foo() -> C1", which) } @@ -139,10 +139,10 @@ Overrides.test("contravariant return type override, base class to derived class" } } - Derived().foo() as C1 + _ = Derived().foo() as C1 expectEqual("Derived.foo() -> C1x", which) - Derived().foo() as C1x + _ = Derived().foo() as C1x expectEqual("Derived.foo() -> C1x", which) } @@ -156,10 +156,10 @@ Overrides.test("contravariant return type override, optional base class to non-o } } - Derived().foo() as C1 + _ = Derived().foo() as C1 expectEqual("Derived.foo() -> C1x", which) - Derived().foo() as C1x + _ = Derived().foo() as C1x expectEqual("Derived.foo() -> C1x", which) } @@ -180,7 +180,7 @@ Overrides.test("contravariant return type override, protocol to protocol") { // Derived().foo() as P1 // error: ambiguous use of 'foo()' // expectEqual("Derived.foo() -> P1x", which) - Derived().foo() as P1x + _ = Derived().foo() as P1x expectEqual("Derived.foo() -> P1x", which) } @@ -201,7 +201,7 @@ Overrides.test("contravariant return type override, protocol to struct") { // Derived().foo() as P1 // error: ambiguous use of 'foo()' // expectEqual("Derived.foo() -> P1ImplS1", which) - Derived().foo() as P1ImplS1 + _ = Derived().foo() as P1ImplS1 expectEqual("Derived.foo() -> P1ImplS1", which) }