rspec/rules/S4056/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

96 lines
2.9 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?
When a ``++System.Globalization.CultureInfo++`` or ``++IFormatProvider++`` object is not supplied, the default value that is supplied by the overloaded member might not have the effect that you want in all locales.
You should supply culture-specific information according to the following guidelines:
* If the value will be displayed to the user, use the current culture. See ``++CultureInfo.CurrentCulture++``.
* If the value will be stored and accessed by software (persisted to a file or database), use the invariant culture. See ``++CultureInfo.InvariantCulture++``.
* If you do not know the destination of the value, have the data consumer or provider specify the culture.
This rule raises an issue when a method or constructor calls one or more members that have overloads that accept a ``++System.IFormatProvider++`` parameter, and the method or constructor does not call the overload that takes the ``++IFormatProvider++`` parameter. This rule ignores calls to .NET Framework methods that are documented as ignoring the ``++IFormatProvider++`` parameter as well as the following methods:
* ``++Activator.CreateInstance++``
* ``++ResourceManager.GetObject++``
* ``++ResourceManager.GetString++``
=== Noncompliant code example
[source,csharp]
----
using System;
namespace MyLibrary
{
public class Foo
{
public void Bar(String string1)
{
if(string.Compare(string1, string2, false) == 0) // Noncompliant
{
Console.WriteLine(string3.ToLower()); // Noncompliant
}
}
}
}
----
=== Compliant solution
[source,csharp]
----
using System;
using System.Globalization;
namespace MyLibrary
{
public class Foo
{
public void Bar(String string1, String string2, String string3)
{
if(string.Compare(string1, string2, false,
CultureInfo.InvariantCulture) == 0)
{
Console.WriteLine(string3.ToLower(CultureInfo.CurrentCulture));
}
}
}
}
----
=== Exceptions
This rule will not raise an issue when the overload is marked as obsolete.
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Use the overload that takes a "CultureInfo" or "IFormatProvider" parameter.
=== Highlighting
Method or constructor call
'''
== Comments And Links
(visible only on this page)
=== on 15 Apr 2019, 10:36:01 Andrei Epure wrote:
\[~nicolas.harraudeau] - should we consider https://docs.microsoft.com/en-us/dotnet/api/system.stringcomparison?view=netframework-4.7.2[StringComparison] as well (because is implies CultureInfo)?
=== on 15 Apr 2019, 18:23:22 Nicolas Harraudeau wrote:
Not for now. We need to dig this a little more. Collation issues have usually less impact than encoding issues. We might have to create a separate rule.
endif::env-github,rspecator-view[]