rspec/rules/S2275/csharp/rule.adoc

91 lines
3.2 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2021-01-27 13:42:22 +01:00
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[])++``.
2020-06-30 12:48:07 +02:00
=== Noncompliant code example
2020-06-30 12:48:07 +02:00
2022-02-04 17:28:24 +01:00
[source,csharp]
2020-06-30 12:48:07 +02:00
----
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
2020-06-30 12:48:07 +02:00
2022-02-04 17:28:24 +01:00
[source,csharp]
2020-06-30 12:48:07 +02:00
----
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
2020-06-30 12:48:07 +02:00
2021-01-27 13:42:22 +01:00
* No issue is raised if the format string is not a ``++const++``.
[source,csharp]
2020-06-30 12:48:07 +02:00
----
var pattern = "{0} {1} {2}";
var res = string.Format(pattern, 1, 2); // Compliant, not const string are not recognized
----
2020-06-30 12:48:07 +02:00
* No issue is raised if the argument is not an inline creation array.
[source,csharp]
2020-06-30 12:48:07 +02:00
----
var array = new int[] {};
var res = string.Format("{0} {1}", array); // Compliant we don't know the size of the array
----
2021-01-27 13:42:22 +01:00
* This rule doesn't check whether the format specifier (defined after the ``++:++``) is actually valid.
2020-06-30 12:48:07 +02:00
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[]