rspec/rules/S2275/csharp/rule.adoc
Fred Tingaud 16f6c0aecf
Inline adoc when include has no additional value (#1940)
Inline adoc files when they are included exactly once.

Also fix language tags because this inlining gives us better information
on what language the code is written in.
2023-05-25 14:18:12 +02:00

91 lines
3.2 KiB
Plaintext
Raw 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?
Because composite format strings are interpreted at runtime, rather than validated by the compiler, they can contain errors that lead to unexpected behaviors or runtime errors. This rule statically validates the good behavior of composite formats when calling the methods of ``++String.Format++``, ``++StringBuilder.AppendFormat++``, ``++Console.Write++``, ``++Console.WriteLine++``, ``++TextWriter.Write++``, ``++TextWriter.WriteLine++``, ``++Debug.WriteLine(String,Object[])++``, ``++Trace.TraceError(String,Object[])++``, ``++Trace.TraceInformation(String,Object[])++``, ``++Trace.TraceWarning(String,Object[])++`` and ``++TraceSource.TraceInformation(String,Object[])++``.
=== Noncompliant code example
[source,csharp]
----
s = string.Format("[0}", arg0);
s = string.Format("{{0}", arg0);
s = string.Format("{0}}", arg0);
s = string.Format("{-1}", arg0);
s = string.Format("{0} {1}", arg0);
----
=== Compliant solution
[source,csharp]
----
s = string.Format("{0}", 42); // Compliant
s = string.Format("{0,10}", 42); // Compliant
s = string.Format("{0,-10}", 42); // Compliant
s = string.Format("{0:0000}", 42); // Compliant
s = string.Format("{2}-{0}-{1}", 1, 2, 3); // Compliant
s = string.Format("no format"); // Compliant
----
=== Exceptions
* No issue is raised if the format string is not a ``++const++``.
[source,csharp]
----
var pattern = "{0} {1} {2}";
var res = string.Format(pattern, 1, 2); // Compliant, not const string are not recognized
----
* No issue is raised if the argument is not an inline creation array.
[source,csharp]
----
var array = new int[] {};
var res = string.Format("{0} {1}", array); // Compliant we don't know the size of the array
----
* This rule doesn't check whether the format specifier (defined after the ``++:++``) is actually valid.
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
* Invalid string format, the format cannot be null.
* Invalid string format, unbalanced curly brace count.
* Invalid string format, opening curly brace can only be followed by a digit or an opening curly brace.
* Invalid string format, the highest string format item index should not be greater than the arguments count.
* Invalid string format, format items should comply with the following pattern '{index[,alignment][:formatString]}'.
* Invalid string format, all format item indexes should be a number.
* Invalid string format, all format item alignments should be a number.
=== Highlighting
The format string.
'''
== Comments And Links
(visible only on this page)
=== on 31 Aug 2015, 12:39:59 Tamas Vajk wrote:
\[~ann.campbell.2] Could you check the issue message? I can't easily find out the number of placeholders in the format, that's why I can't use the same as in Python.
=== on 31 Aug 2015, 13:03:46 Ann Campbell wrote:
\[~tamas.vajk] the message is fine as far as it goes, but it doesn't cover something like this:
----
var s1 = string.Format("{3}", 1);
----
=== on 18 Jan 2016, 11:05:13 Ann Campbell wrote:
This should eventually cover ``++Console.WriteLine++`` (and some others, I'm told) too.
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]