diff --git a/docs/pages/pmd/rules/swift.md b/docs/pages/pmd/rules/swift.md index 47e34a46c7..9bce958a67 100644 --- a/docs/pages/pmd/rules/swift.md +++ b/docs/pages/pmd/rules/swift.md @@ -11,11 +11,12 @@ folder: pmd/rules {% include callout.html content="Rules which enforce generally accepted best practices." %} -* [ProhibitedInterfaceBuilder](pmd_rules_swift_bestpractices.html#prohibitedinterfacebuilder): Creating views using Interface Builder should be avoided. +* [ProhibitedInterfaceBuilder](pmd_rules_swift_bestpractices.html#prohibitedinterfacebuilder): Creating views using Interface Builder should be avoided. Defining views by code allow... ## Error Prone {% include callout.html content="Rules to detect constructs that are either broken, extremely confusing or prone to runtime errors." %} -* [ForceTry](pmd_rules_swift_errorprone.html#forcetry): Force tries should be avoided. +* [ForceCast](pmd_rules_swift_errorprone.html#forcecast): Force casts should be avoided. This may lead to a crash if it's not used carefully. Fo... +* [ForceTry](pmd_rules_swift_errorprone.html#forcetry): Force tries should be avoided. If the code being wrapped happens to raise and exception, our appl... diff --git a/docs/pages/pmd/rules/swift/bestpractices.md b/docs/pages/pmd/rules/swift/bestpractices.md index abc5bab0c7..280901e7fc 100644 --- a/docs/pages/pmd/rules/swift/bestpractices.md +++ b/docs/pages/pmd/rules/swift/bestpractices.md @@ -16,9 +16,24 @@ language: Swift **Priority:** Medium High (2) Creating views using Interface Builder should be avoided. +Defining views by code allows the compiler to detect issues that otherwise will be runtime errors. +It's difficult to review the auto-generated code and allow concurrent modifications of those files. +Consider building views programmatically. **This rule is defined by the following Java class:** [net.sourceforge.pmd.lang.swift.rule.bestpractices.ProhibitedInterfaceBuilderRule](https://github.com/pmd/pmd/blob/master/pmd-swift/src/main/java/net/sourceforge/pmd/lang/swift/rule/bestpractices/ProhibitedInterfaceBuilderRule.java) +**Example(s):** + +``` swift +{%raw%}class ViewController: UIViewController { + @IBOutlet var label: UILabel! // violation, referencing a IBOutlet +} + +class ViewController: UIViewController { + var label: UILabel! +}{%endraw%} +``` + **Use this rule by referencing it:** ``` xml diff --git a/docs/pages/pmd/rules/swift/errorprone.md b/docs/pages/pmd/rules/swift/errorprone.md index d94255a80a..555ff0a4ed 100644 --- a/docs/pages/pmd/rules/swift/errorprone.md +++ b/docs/pages/pmd/rules/swift/errorprone.md @@ -5,23 +5,60 @@ permalink: pmd_rules_swift_errorprone.html folder: pmd/rules/swift sidebaractiveurl: /pmd_rules_swift.html editmepath: ../pmd-swift/src/main/resources/category/swift/errorprone.xml -keywords: Error Prone, ForceTry +keywords: Error Prone, ForceCast, ForceTry language: Swift --- +## ForceCast + +**Since:** PMD 7.0 + +**Priority:** Medium (3) + +Force casts should be avoided. This may lead to a crash if it's not used carefully. +For example assuming a JSON property has a given type, or your reused Cell has a certain contract. +Consider using conditional casting and handling the resulting optional. + +**This rule is defined by the following XPath expression:** +``` xpath +//TypeCastingOperatorContext[starts-with(@Text,'as!')] +``` + +**Example(s):** + +``` swift +{%raw%}NSNumber() as! Int // violation, force casting + +NSNumber() as? Int // no violation{%endraw%} +``` + +**Use this rule by referencing it:** +``` xml + +``` + ## ForceTry **Since:** PMD 7.0 **Priority:** Medium (3) -Force tries should be avoided. +Force tries should be avoided. If the code being wrapped happens to raise and exception, our application will crash. +Consider using a conditional try and handling the resulting optional, or wrapping the try statement in a do-catch block. **This rule is defined by the following XPath expression:** ``` xpath //TryOperatorContext[@Text='try!'] ``` +**Example(s):** + +``` swift +{%raw%}let x = try! someThrowingFunction() // violation, force trying + +let x = try? someThrowingFunction() // no violation{%endraw%} +``` + **Use this rule by referencing it:** ``` xml diff --git a/pmd-swift/src/main/resources/category/swift/bestpractices.xml b/pmd-swift/src/main/resources/category/swift/bestpractices.xml index 194910248e..0b6067994f 100644 --- a/pmd-swift/src/main/resources/category/swift/bestpractices.xml +++ b/pmd-swift/src/main/resources/category/swift/bestpractices.xml @@ -18,7 +18,19 @@ Rules which enforce generally accepted best practices. Creating views using Interface Builder should be avoided. Defining views by code allows the compiler to detect issues that otherwise will be runtime errors. It's difficult to review the auto-generated code and allow concurrent modifications of those files. + Consider building views programmatically. 2 + + + diff --git a/pmd-swift/src/main/resources/category/swift/errorprone.xml b/pmd-swift/src/main/resources/category/swift/errorprone.xml index 857555d3be..f1f0c6e015 100644 --- a/pmd-swift/src/main/resources/category/swift/errorprone.xml +++ b/pmd-swift/src/main/resources/category/swift/errorprone.xml @@ -18,6 +18,7 @@ Force casts should be avoided. This may lead to a crash if it's not used carefully. For example assuming a JSON property has a given type, or your reused Cell has a certain contract. + Consider using conditional casting and handling the resulting optional. 3 @@ -30,6 +31,13 @@ + + + Force tries should be avoided. If the code being wrapped happens to raise and exception, our application will crash. + Consider using a conditional try and handling the resulting optional, or wrapping the try statement in a do-catch block. 3 @@ -52,5 +61,12 @@ + + + \ No newline at end of file