66 lines
2.6 KiB
Plaintext
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

== Why is this an issue?
Non-encoded https://en.wikipedia.org/wiki/Control_character[control characters] and whitespace characters are often injected in the source code because of a bad manipulation. They are either invisible or difficult to recognize, which can result in bugs when the string is not what the developer expects. If you actually need to use a control character use their encoded version:
* https://en.wikipedia.org/wiki/ASCII[ASCII], for example `\n` and `\t`
* https://en.wikipedia.org/wiki/Unicode[Unicode], for example `U+000D` and `U+0009`
This rule raises an issue when the following characters are seen in a https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/strings/[string literal]:
* https://en.wikipedia.org/wiki/ASCII#Control_characters[ASCII control character]. (character index < 32 or ++=++ 127)
* Unicode https://en.wikipedia.org/wiki/Unicode_character_property#Whitespace[whitespace characters].
* Unicode https://en.wikipedia.org/wiki/C0_and_C1_control_codes[C0 control characters]
* Unicode characters ``++U+200B, U+200C, U+200D, U+2060, U+FEFF, U+2028, U+2029++``
=== Exceptions
* https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/strings/#verbatim-string-literals[Verbatim string literals] and https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/strings/#raw-string-literals[raw string literals], since they have no escape mechanism
* The simple space character: Unicode `U+0020`, ASCII 32
== How to fix it
=== Code examples
==== Noncompliant code example
[source,csharp,diff-id=1,diff-type=noncompliant]
----
string tabInside = "A B"; // Noncompliant: contains a tabulation
string zeroWidthSpaceInside = "foobar"; // Noncompliant: contains a U+200B character inside
Console.WriteLine(zeroWidthSpaceInside); // Prints "foo?bar"
----
==== Compliant solution
[source,csharp,diff-id=1,diff-type=compliant]
----
string tabInside = "A\tB"; // Compliant: escaped value
string zeroWidthSpaceInside = "foo\u200Bbar"; // Compliant: escaped value
Console.WriteLine(zeroWidthSpaceInside); // Prints "foo?bar"
----
== Resources
=== Documentation
* https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/strings/[Strings and string literals]
* https://en.wikipedia.org/wiki/Control_character[Control character]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Replace the control character at position XX by its escape sequence 'XX'.
'''
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]