Skip to content

Commit

Permalink
Enable recently added Microsoft.CodeAnalysis.NetAnalyzers rules (#53158)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephentoub authored May 28, 2021
1 parent 4387828 commit 72ff0df
Show file tree
Hide file tree
Showing 29 changed files with 103 additions and 75 deletions.
9 changes: 9 additions & 0 deletions eng/CodeAnalysis.ruleset
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
<Rule Id="CA1307" Action="None" /> <!-- Specify StringComparison -->
<Rule Id="CA1308" Action="None" /> <!-- Normalize strings to uppercase -->
<Rule Id="CA1309" Action="None" /> <!-- Use ordinal stringcomparison -->
<Rule Id="CA1310" Action="Info" /> <!-- Specify StringComparison for correctness -->
<Rule Id="CA1401" Action="Warning" /> <!-- P/Invokes should not be visible -->
<Rule Id="CA1416" Action="Warning" /> <!-- Validate platform compatibility -->
<Rule Id="CA1417" Action="Warning" /> <!-- Do not use 'OutAttribute' on string parameters for P/Invokes -->
Expand Down Expand Up @@ -119,6 +120,12 @@
<Rule Id="CA1838" Action="Warning" /> <!-- Avoid 'StringBuilder' parameters for P/Invokes -->
<Rule Id="CA1839" Action="Warning" /> <!-- Use 'Environment.ProcessPath' -->
<Rule Id="CA1840" Action="Warning" /> <!-- Use 'Environment.CurrentManagedThreadId' -->
<Rule Id="CA1841" Action="Warning" /> <!-- Prefer Dictionary.Contains methods -->
<Rule Id="CA1842" Action="Warning" /> <!-- Do not use 'WhenAll' with a single task -->
<Rule Id="CA1843" Action="Warning" /> <!-- Do not use 'WaitAll' with a single task -->
<Rule Id="CA1844" Action="Warning" /> <!-- Provide memory-based overrides of async methods when subclassing 'Stream' -->
<Rule Id="CA1845" Action="Warning" /> <!-- Use span-based 'string.Concat' -->
<Rule Id="CA1846" Action="Warning" /> <!-- Prefer 'AsSpan' over 'Substring' -->
<Rule Id="CA2000" Action="None" /> <!-- Dispose objects before losing scope -->
<Rule Id="CA2002" Action="None" /> <!-- Do not lock on objects with weak identity -->
<Rule Id="CA2007" Action="Warning" /> <!-- Consider calling ConfigureAwait on the awaited task -->
Expand Down Expand Up @@ -165,6 +172,8 @@
<Rule Id="CA2247" Action="Warning" /> <!-- Argument passed to TaskCompletionSource constructor should be TaskCreationOptions enum instead of TaskContinuationOptions enum. -->
<Rule Id="CA2248" Action="Warning" /> <!-- Provide correct 'enum' argument to 'Enum.HasFlag' -->
<Rule Id="CA2249" Action="Warning" /> <!-- Consider using 'string.Contains' instead of 'string.IndexOf' -->
<Rule Id="CA2250" Action="Warning" /> <!-- Use 'ThrowIfCancellationRequested' -->
<Rule Id="CA2251" Action="Warning" /> <!-- Use 'string.Equals' -->
<Rule Id="CA2300" Action="None" /> <!-- Do not use insecure deserializer BinaryFormatter -->
<Rule Id="CA2301" Action="None" /> <!-- Do not call BinaryFormatter.Deserialize without first setting BinaryFormatter.Binder -->
<Rule Id="CA2302" Action="None" /> <!-- Ensure BinaryFormatter.Binder is set before calling BinaryFormatter.Deserialize -->
Expand Down
9 changes: 9 additions & 0 deletions eng/CodeAnalysis.test.ruleset
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
<Rule Id="CA1307" Action="None" /> <!-- Specify StringComparison -->
<Rule Id="CA1308" Action="None" /> <!-- Normalize strings to uppercase -->
<Rule Id="CA1309" Action="None" /> <!-- Use ordinal stringcomparison -->
<Rule Id="CA1310" Action="None" /> <!-- Specify StringComparison for correctness -->
<Rule Id="CA1401" Action="None" /> <!-- P/Invokes should not be visible -->
<Rule Id="CA1416" Action="None" /> <!-- Validate platform compatibility -->
<Rule Id="CA1417" Action="None" /> <!-- Do not use 'OutAttribute' on string parameters for P/Invokes -->
Expand Down Expand Up @@ -118,6 +119,12 @@
<Rule Id="CA1838" Action="None" /> <!-- Avoid 'StringBuilder' parameters for P/Invokes -->
<Rule Id="CA1839" Action="None" /> <!-- Use 'Environment.ProcessPath' -->
<Rule Id="CA1840" Action="None" /> <!-- Use 'Environment.CurrentManagedThreadId' -->
<Rule Id="CA1841" Action="None" /> <!-- Prefer Dictionary.Contains methods -->
<Rule Id="CA1842" Action="None" /> <!-- Do not use 'WhenAll' with a single task -->
<Rule Id="CA1843" Action="None" /> <!-- Do not use 'WaitAll' with a single task -->
<Rule Id="CA1844" Action="None" /> <!-- Provide memory-based overrides of async methods when subclassing 'Stream' -->
<Rule Id="CA1845" Action="None" /> <!-- Use span-based 'string.Concat' -->
<Rule Id="CA1846" Action="None" /> <!-- Prefer 'AsSpan' over 'Substring' -->
<Rule Id="CA2000" Action="None" /> <!-- Dispose objects before losing scope -->
<Rule Id="CA2002" Action="None" /> <!-- Do not lock on objects with weak identity -->
<Rule Id="CA2007" Action="None" /> <!-- Consider calling ConfigureAwait on the awaited task -->
Expand Down Expand Up @@ -164,6 +171,8 @@
<Rule Id="CA2247" Action="None" /> <!-- Argument passed to TaskCompletionSource constructor should be TaskCreationOptions enum instead of TaskContinuationOptions enum. -->
<Rule Id="CA2248" Action="None" /> <!-- Provide correct 'enum' argument to 'Enum.HasFlag' -->
<Rule Id="CA2249" Action="None" /> <!-- Consider using 'string.Contains' instead of 'string.IndexOf' -->
<Rule Id="CA2250" Action="None" /> <!-- Use 'ThrowIfCancellationRequested' -->
<Rule Id="CA2251" Action="None" /> <!-- Use 'string.Equals' -->
<Rule Id="CA2300" Action="None" /> <!-- Do not use insecure deserializer BinaryFormatter -->
<Rule Id="CA2301" Action="None" /> <!-- Do not call BinaryFormatter.Deserialize without first setting BinaryFormatter.Binder -->
<Rule Id="CA2302" Action="None" /> <!-- Ensure BinaryFormatter.Binder is set before calling BinaryFormatter.Deserialize -->
Expand Down
2 changes: 1 addition & 1 deletion eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
<MicrosoftCodeAnalysisVersion>3.8.0</MicrosoftCodeAnalysisVersion>
</PropertyGroup>
<PropertyGroup>
<MicrosoftCodeAnalysisNetAnalyzersVersion>6.0.0-preview5.21262.4</MicrosoftCodeAnalysisNetAnalyzersVersion>
<MicrosoftCodeAnalysisNetAnalyzersVersion>6.0.0-preview6.21274.2</MicrosoftCodeAnalysisNetAnalyzersVersion>
<MicrosoftCodeAnalysisCSharpCodeStyleVersion>3.10.0-2.final</MicrosoftCodeAnalysisCSharpCodeStyleVersion>
<MicrosoftCodeAnalysisCSharpVersion>3.10.0-2.final</MicrosoftCodeAnalysisCSharpVersion>
<!-- Arcade dependencies -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,9 @@ public bool GetProxyForUrl(
//
// We match behavior of WINHTTP_ACCESS_TYPE_AUTOMATIC_PROXY and ignore errors.

string destination = uri.AbsoluteUri;
#pragma warning disable CA1845 // file is shared with a build that lacks string.Concat for spans
// Underlying code does not understand WebSockets so we need to convert it to http or https.
string destination = uri.AbsoluteUri;
if (uri.Scheme == UriScheme.Wss)
{
destination = UriScheme.Https + destination.Substring(UriScheme.Wss.Length);
Expand All @@ -128,6 +129,7 @@ public bool GetProxyForUrl(
{
destination = UriScheme.Http + destination.Substring(UriScheme.Ws.Length);
}
#pragma warning restore CA1845

var repeat = false;
do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1537,8 +1537,7 @@ Namespace Microsoft.VisualBasic.FileIO
' Remove any separators at the end for the same reason in IsRoot.
Path1 = Path1.TrimEnd(IO.Path.DirectorySeparatorChar, IO.Path.AltDirectorySeparatorChar)
Path2 = Path2.TrimEnd(IO.Path.DirectorySeparatorChar, IO.Path.AltDirectorySeparatorChar)
Return String.Compare(IO.Path.GetPathRoot(Path1), IO.Path.GetPathRoot(Path2),
StringComparison.OrdinalIgnoreCase) = 0
Return String.Equals(IO.Path.GetPathRoot(Path1), IO.Path.GetPathRoot(Path2), StringComparison.OrdinalIgnoreCase)
End Function

''' <summary>
Expand All @@ -1559,8 +1558,7 @@ Namespace Microsoft.VisualBasic.FileIO
End If

Path = Path.TrimEnd(IO.Path.DirectorySeparatorChar, IO.Path.AltDirectorySeparatorChar)
Return String.Compare(Path, IO.Path.GetPathRoot(Path),
StringComparison.OrdinalIgnoreCase) = 0
Return String.Equals(Path, IO.Path.GetPathRoot(Path), StringComparison.OrdinalIgnoreCase)
End Function

''' <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1233,7 +1233,7 @@ Namespace Microsoft.VisualBasic.FileIO
If Token <> "" Then
If m_HasFieldsEnclosedInQuotes And m_TextFieldType = FieldType.Delimited Then

If String.Compare(Token.Trim(), """", StringComparison.Ordinal) = 0 Then
If String.Equals(Token.Trim(), """", StringComparison.Ordinal) Then
Throw GetInvalidOperationException(SR.TextFieldParser_InvalidComment)
End If
End If
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1355,7 +1355,7 @@ Namespace Microsoft.VisualBasic
' compare the filename with the input string case insensitive
' exit loop if match occurs and both files are not sequential input
' and not random/binary.
If System.String.Compare(sPath, oFile.GetAbsolutePath(), StringComparison.OrdinalIgnoreCase) = 0 Then
If System.String.Equals(sPath, oFile.GetAbsolutePath(), StringComparison.OrdinalIgnoreCase) Then
' If path is the same, then verify
' that neither file is open for sequential input
' and that both are open for the same mode (either Binary or Random)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ Namespace Microsoft.VisualBasic
ServerName = Nothing
Else
'Does the ServerName match the MachineName?
If String.Compare(Environment.MachineName, ServerName, StringComparison.OrdinalIgnoreCase) = 0 Then
If String.Equals(Environment.MachineName, ServerName, StringComparison.OrdinalIgnoreCase) Then
ServerName = Nothing
End If
End If
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1075,7 +1075,7 @@ RedimAndExit:
Select Case Style.Chars(0)
'(F)ixed
Case "f"c, "F"c
If String.Compare(Style, NAMEDFORMAT_FIXED, StringComparison.OrdinalIgnoreCase) = 0 Then
If String.Equals(Style, NAMEDFORMAT_FIXED, StringComparison.OrdinalIgnoreCase) Then
ReturnValue = CDbl(Expression).ToString("0.00", Nothing)
Return True
End If
Expand All @@ -1087,13 +1087,13 @@ RedimAndExit:
'(O)n/off
Select Case Style.Chars(0)
Case "y"c, "Y"c
If String.Compare(Style, NAMEDFORMAT_YES_NO, StringComparison.OrdinalIgnoreCase) = 0 Then
If String.Equals(Style, NAMEDFORMAT_YES_NO, StringComparison.OrdinalIgnoreCase) Then
ReturnValue = CInt(CBool(Expression)).ToString(CachedYesNoFormatStyle, Nothing)
Return True
End If

Case "o"c, "O"c
If String.Compare(Style, NAMEDFORMAT_ON_OFF, StringComparison.OrdinalIgnoreCase) = 0 Then
If String.Equals(Style, NAMEDFORMAT_ON_OFF, StringComparison.OrdinalIgnoreCase) Then
ReturnValue = CInt(CBool(Expression)).ToString(CachedOnOffFormatStyle, Nothing)
Return True
End If
Expand All @@ -1104,7 +1104,7 @@ RedimAndExit:
'(P)ercent
Select Case Style.Chars(0)
Case "p"c, "P"c
If String.Compare(Style, NAMEDFORMAT_PERCENT, StringComparison.OrdinalIgnoreCase) = 0 Then
If String.Equals(Style, NAMEDFORMAT_PERCENT, StringComparison.OrdinalIgnoreCase) Then
ReturnValue = CDbl(Expression).ToString("0.00%", Nothing)
Return True
End If
Expand All @@ -1117,12 +1117,12 @@ RedimAndExit:

Select Case Style.Chars(0)
Case "s"c, "S"c
If String.Compare(Style, NAMEDFORMAT_STANDARD, StringComparison.OrdinalIgnoreCase) = 0 Then
If String.Equals(Style, NAMEDFORMAT_STANDARD, StringComparison.OrdinalIgnoreCase) Then
ReturnValue = CDbl(Expression).ToString("N2", Nothing)
Return True
End If
Case "c"c, "C"c
If String.Compare(Style, NAMEDFORMAT_CURRENCY, StringComparison.OrdinalIgnoreCase) = 0 Then
If String.Equals(Style, NAMEDFORMAT_CURRENCY, StringComparison.OrdinalIgnoreCase) Then
ReturnValue = CDbl(Expression).ToString("C", Nothing)
Return True
End If
Expand All @@ -1135,13 +1135,13 @@ RedimAndExit:

Select Case Style.Chars(5)
Case "t"c, "T"c
If String.Compare(Style, NAMEDFORMAT_LONG_TIME, StringComparison.OrdinalIgnoreCase) = 0 Then
If String.Equals(Style, NAMEDFORMAT_LONG_TIME, StringComparison.OrdinalIgnoreCase) Then
ReturnValue = CDate(Expression).ToString("T", Nothing)
Return True
End If

Case "d"c, "D"c
If String.Compare(Style, NAMEDFORMAT_LONG_DATE, StringComparison.OrdinalIgnoreCase) = 0 Then
If String.Equals(Style, NAMEDFORMAT_LONG_DATE, StringComparison.OrdinalIgnoreCase) Then
ReturnValue = CDate(Expression).ToString("D", Nothing)
Return True
End If
Expand All @@ -1156,25 +1156,25 @@ RedimAndExit:

Select Case Style.Chars(6)
Case "a"c, "A"c
If String.Compare(Style, NAMEDFORMAT_TRUE_FALSE, StringComparison.OrdinalIgnoreCase) = 0 Then
If String.Equals(Style, NAMEDFORMAT_TRUE_FALSE, StringComparison.OrdinalIgnoreCase) Then
ReturnValue = CInt(CBool(Expression)).ToString(CachedTrueFalseFormatStyle, Nothing)
Return True
End If

Case "t"c, "T"c
If String.Compare(Style, NAMEDFORMAT_SHORT_TIME, StringComparison.OrdinalIgnoreCase) = 0 Then
If String.Equals(Style, NAMEDFORMAT_SHORT_TIME, StringComparison.OrdinalIgnoreCase) Then
ReturnValue = CDate(Expression).ToString("t", Nothing)
Return True
End If

Case "d"c, "D"c
If String.Compare(Style, NAMEDFORMAT_SHORT_DATE, StringComparison.OrdinalIgnoreCase) = 0 Then
If String.Equals(Style, NAMEDFORMAT_SHORT_DATE, StringComparison.OrdinalIgnoreCase) Then
ReturnValue = CDate(Expression).ToString("d", Nothing)
Return True
End If

Case "i"c, "I"c
If String.Compare(Style, NAMEDFORMAT_SCIENTIFIC, StringComparison.OrdinalIgnoreCase) = 0 Then
If String.Equals(Style, NAMEDFORMAT_SCIENTIFIC, StringComparison.OrdinalIgnoreCase) Then
Dim dbl As Double
dbl = CDbl(Expression)
If System.Double.IsNaN(dbl) OrElse System.Double.IsInfinity(dbl) Then
Expand All @@ -1194,13 +1194,13 @@ RedimAndExit:

Select Case Style.Chars(7)
Case "t"c, "T"c
If String.Compare(Style, NAMEDFORMAT_MEDIUM_TIME, StringComparison.OrdinalIgnoreCase) = 0 Then
If String.Equals(Style, NAMEDFORMAT_MEDIUM_TIME, StringComparison.OrdinalIgnoreCase) Then
ReturnValue = CDate(Expression).ToString("T", Nothing)
Return True
End If

Case "d"c, "D"c
If String.Compare(Style, NAMEDFORMAT_MEDIUM_DATE, StringComparison.OrdinalIgnoreCase) = 0 Then
If String.Equals(Style, NAMEDFORMAT_MEDIUM_DATE, StringComparison.OrdinalIgnoreCase) Then
ReturnValue = CDate(Expression).ToString("D", Nothing)
Return True
End If
Expand All @@ -1209,7 +1209,7 @@ RedimAndExit:
Case 12
Select Case Style.Chars(0)
Case "g"c, "G"c
If String.Compare(Style, NAMEDFORMAT_GENERAL_DATE, StringComparison.OrdinalIgnoreCase) = 0 Then
If String.Equals(Style, NAMEDFORMAT_GENERAL_DATE, StringComparison.OrdinalIgnoreCase) Then
ReturnValue = CDate(Expression).ToString("G", Nothing)
Return True
End If
Expand All @@ -1218,7 +1218,7 @@ RedimAndExit:
Case 14
Select Case Style.Chars(0)
Case "g"c, "G"c
If String.Compare(Style, NAMEDFORMAT_GENERAL_NUMBER, StringComparison.OrdinalIgnoreCase) = 0 Then
If String.Equals(Style, NAMEDFORMAT_GENERAL_NUMBER, StringComparison.OrdinalIgnoreCase) Then
ReturnValue = CDbl(Expression).ToString("G", Nothing)
Return True
End If
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ internal static void AppendKeyValuePairBuilder(StringBuilder builder, string key
{
if ((0 < keyValue.Length) &&
// string.Contains(char) is .NetCore2.1+ specific
(('{' == keyValue[0]) || (0 <= keyValue.IndexOf(';')) || (0 == string.Compare(DbConnectionStringKeywords.Driver, keyName, StringComparison.OrdinalIgnoreCase))) &&
(('{' == keyValue[0]) || (0 <= keyValue.IndexOf(';')) || (string.Equals(DbConnectionStringKeywords.Driver, keyName, StringComparison.OrdinalIgnoreCase))) &&
!s_connectionStringQuoteOdbcValueRegex.IsMatch(keyValue))
{
// always quote Driver value (required for ODBC Version 2.65 and earlier)
Expand Down
2 changes: 1 addition & 1 deletion src/libraries/System.Data.OleDb/src/DbConnectionOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ internal static void AppendKeyValuePairBuilder(StringBuilder builder, string key
if (useOdbcRules)
{
if ((0 < keyValue.Length) &&
(('{' == keyValue[0]) || (0 <= keyValue.IndexOf(';')) || (0 == string.Compare(DbConnectionStringKeywords.Driver, keyName, StringComparison.OrdinalIgnoreCase))) &&
(('{' == keyValue[0]) || (0 <= keyValue.IndexOf(';')) || (string.Equals(DbConnectionStringKeywords.Driver, keyName, StringComparison.OrdinalIgnoreCase))) &&
!ConnectionStringQuoteOdbcValueRegex.IsMatch(keyValue))
{
// always quote Driver value (required for ODBC Version 2.65 and earlier)
Expand Down
Loading

0 comments on commit 72ff0df

Please sign in to comment.