rspec/rules/S3661/swift/rule.adoc

50 lines
990 B
Plaintext
Raw Normal View History

2021-04-28 16:49:39 +02:00
The use of Swift 2.0's ``++try!++`` lets you execute code that might throw an exception without using the ``++do++`` and ``++catch++`` syntax normally required for such code. By using it, you're guaranteeing that the executed code will never fail. Murphy's Law guarantees you're wrong. And when it does fail, the program will exit abruptly, probably without cleaning up after itself.
2021-04-28 16:49:39 +02:00
== Noncompliant Code Example
2022-02-04 17:28:24 +01:00
[source,swift]
2021-04-28 16:49:39 +02:00
----
let myvar = try! dangerousCode(foo); // Noncompliant
// ...
----
2021-04-28 16:49:39 +02:00
== Compliant Solution
2022-02-04 17:28:24 +01:00
[source,swift]
2021-04-28 16:49:39 +02:00
----
guard let myvar = try? dangerousCode(foo) else {
// handle error
}
// or
if let myvar = try? dangerousCode(foo); {
// ...
} else {
// handle error
}
// or
do {
let myvar = try dangerousCode(foo)
// ...
} catch {
// handle error
}
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::message.adoc[]
include::highlighting.adoc[]
endif::env-github,rspecator-view[]