rspec/rules/S1858/csharp/rule.adoc
Alban Auzeill 2c306d110e Fix code block ambiguity with old header style
Ensure blank line before list and clean the one leading space
2020-06-30 17:16:12 +02:00

36 lines
1.1 KiB
Plaintext

Invoking a method designed to return a string representation of an object which is already a string is a waste of keystrokes. Similarly, explicitly invoking <code>ToString()</code> when the compiler would do it implicitly is also needless code-bloat.
This rule raises an issue when <code>ToString()</code> is invoked:
* on a <code>string</code>
* on a non-<code>string</code> operand to concatenation
* on an argument to <code>string.Format</code>
== Noncompliant Code Example
----
var s = "foo";
var t = "fee fie foe " + s.ToString(); // Noncompliant
var someObject = new object();
var u = "" + someObject.ToString(); // Noncompliant
var v = string.Format("{0}", someObject.ToString()); // Noncompliant
----
== Compliant Solution
----
var s = "foo";
var t = "fee fie foe " + s;
var someObject = new object();
var u = "" + someObject;
var v = string.Format("{0}", someObject);
----
== Exceptions
The rule does not report on value types, where leaving off the <code>ToString()</code> call would result in automatic boxing.
----
var v = string.Format("{0}", 1.ToString());
----