rspec/rules/S2961/swift/rule.adoc
Fred Tingaud d3cfe19d7e
Fix broken or dangerous backquotes
Co-authored-by: Marco Borgeaud <89914223+marco-antognini-sonarsource@users.noreply.github.com>
2023-10-30 10:33:56 +01:00

96 lines
2.5 KiB
Plaintext

== Why is this an issue?
You can't create a variable named "for". Unless you put backticks (``++`++``) around it.
Since that would be the first step down a slippery slope of hopeless confusion, backticks should be removed from identifier names - whether they're keywords or not - and the identifiers renamed as required.
=== Noncompliant code example
[source,swift]
----
var `for` = 1 // Noncompliant
for (var `in` = 0; `in` < 10 && `for` > 0; `in`++) { // Noncompliant
// ...
}
var `x` = "hello" // Noncompliant; why would you do this?
----
=== Compliant solution
[source,swift]
----
var i = a
for (var j=0; j< 10; j++) {
// ...
}
var x = "hello"
----
=== Exceptions
When Objective-C libraries are used in Swift, backticks may be needed around parameter names which are keywords in Swift but not in Objective C. Therefore this rule ignores backticks around parameter names.
[source,swift]
----
var protectionSpace: NSURLProtectionSpace = NSURLProtectionSpace(
host: host,
port: port,
`protocol`: prot, // Compliant
realm: nil,
authenticationMethod: authenticationMethod
);
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Remove backticks ``++`++`` from "xxx".
Remove backticks ``++`++`` from "xxx" and rename it.
'''
== Comments And Links
(visible only on this page)
=== relates to: S1669
=== on 21 May 2015, 08:29:57 Elena Vilchik wrote:
\[~ann.campbell.2] Could you add something about not keyword identifiers used with back ticks.
----
var x = 1
print(`x`) // Noncompliant, confuses the reader, no reason to do that
----
=== on 21 May 2015, 11:38:32 Ann Campbell wrote:
see what you think now [~elena.vilchik]
=== on 21 May 2015, 11:43:15 Elena Vilchik wrote:
\[~ann.campbell.2] great!
=== on 24 Jun 2015, 10:14:08 Elena Vilchik wrote:
\[~ann.campbell.2] I have 0 issues on ruling for this rule, so backticks are not really used. So I decided to raise issue on every usage of backticked identifier instead of declaration or first backticked usage (as it will not produce a lot of noise and very-very simple to implement). According with it I've updated message and SQALE. Are you ok with that?
=== on 24 Jun 2015, 12:02:44 Elena Vilchik wrote:
\[~ann.campbell.2] Could you translate "Exceptions" block in normal english language?:)
=== on 24 Jun 2015, 13:54:01 Ann Campbell wrote:
Reworded (so double-check me), but it wasn't bad too start with [~elena.vilchik] :).
endif::env-github,rspecator-view[]