Skip to content

Commit 972fa0a

Browse files
Runtime90357 6012 (#6885)
* Fix IndexOfOfRangeException for non-formatting methods. * Check format specification on non-format methods * Use different diagnostic message if format is malformed. * Fix bug where rule was not taking actual index values into account * Running 'msbuild /t:pack'
1 parent 4efa61a commit 972fa0a

17 files changed

+187
-10
lines changed

src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,9 @@
290290
<data name="ProvideCorrectArgumentsToFormattingMethodsMessage" xml:space="preserve">
291291
<value>Provide correct arguments to formatting methods</value>
292292
</data>
293+
<data name="ProvideCorrectArgumentsToFormattingMethodsInvalidFormatMessage" xml:space="preserve">
294+
<value>The format argument is not a valid format string</value>
295+
</data>
293296
<data name="TestForNaNCorrectlyTitle" xml:space="preserve">
294297
<value>Test for NaN correctly</value>
295298
</data>

src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/ProvideCorrectArgumentsToFormattingMethods.cs

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Collections.Generic;
66
using System.Collections.Immutable;
77
using System.Diagnostics.CodeAnalysis;
8+
using System.Linq;
89
using Analyzer.Utilities;
910
using Analyzer.Utilities.Extensions;
1011
using Microsoft.CodeAnalysis;
@@ -23,7 +24,7 @@ public class ProvideCorrectArgumentsToFormattingMethodsAnalyzer : DiagnosticAnal
2324
{
2425
internal const string RuleId = "CA2241";
2526

26-
internal static readonly DiagnosticDescriptor Rule = DiagnosticDescriptorHelper.Create(
27+
internal static readonly DiagnosticDescriptor ArgumentCountRule = DiagnosticDescriptorHelper.Create(
2728
RuleId,
2829
CreateLocalizableResourceString(nameof(ProvideCorrectArgumentsToFormattingMethodsTitle)),
2930
CreateLocalizableResourceString(nameof(ProvideCorrectArgumentsToFormattingMethodsMessage)),
@@ -33,7 +34,18 @@ public class ProvideCorrectArgumentsToFormattingMethodsAnalyzer : DiagnosticAnal
3334
isPortedFxCopRule: true,
3435
isDataflowRule: false);
3536

36-
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(Rule);
37+
internal static readonly DiagnosticDescriptor InvalidFormatRule = DiagnosticDescriptorHelper.Create(
38+
RuleId,
39+
CreateLocalizableResourceString(nameof(ProvideCorrectArgumentsToFormattingMethodsTitle)),
40+
CreateLocalizableResourceString(nameof(ProvideCorrectArgumentsToFormattingMethodsInvalidFormatMessage)),
41+
DiagnosticCategory.Usage,
42+
RuleLevel.BuildWarningCandidate,
43+
description: CreateLocalizableResourceString(nameof(ProvideCorrectArgumentsToFormattingMethodsDescription)),
44+
isPortedFxCopRule: true,
45+
isDataflowRule: false);
46+
47+
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } =
48+
ImmutableArray.Create(ArgumentCountRule, InvalidFormatRule);
3749

3850
public override void Initialize(AnalysisContext context)
3951
{
@@ -65,6 +77,13 @@ public override void Initialize(AnalysisContext context)
6577

6678
int expectedStringFormatArgumentCount = GetFormattingArguments(stringFormat);
6779

80+
if (expectedStringFormatArgumentCount < 0)
81+
{
82+
// Malformed format specification
83+
operationContext.ReportDiagnostic(operationContext.Operation.Syntax.CreateDiagnostic(InvalidFormatRule));
84+
return;
85+
}
86+
6887
// explicit parameter case
6988
if (info.ExpectedStringFormatArgumentCount >= 0)
7089
{
@@ -77,11 +96,16 @@ public override void Initialize(AnalysisContext context)
7796

7897
if (info.ExpectedStringFormatArgumentCount != expectedStringFormatArgumentCount)
7998
{
80-
operationContext.ReportDiagnostic(operationContext.Operation.Syntax.CreateDiagnostic(Rule));
99+
operationContext.ReportDiagnostic(operationContext.Operation.Syntax.CreateDiagnostic(ArgumentCountRule));
81100
}
82101

83102
return;
84103
}
104+
else if (info.ExpectedStringFormatArgumentCount == -2)
105+
{
106+
// Not a formatting method, we only checked the format specification.
107+
return;
108+
}
85109

86110
// ensure argument is an array
87111
IArgumentOperation paramsArgument = invocation.Arguments[info.FormatStringIndex + 1];
@@ -111,7 +135,7 @@ public override void Initialize(AnalysisContext context)
111135
int actualArgumentCount = initializer.ElementValues.Length;
112136
if (actualArgumentCount != expectedStringFormatArgumentCount)
113137
{
114-
operationContext.ReportDiagnostic(operationContext.Operation.Syntax.CreateDiagnostic(Rule));
138+
operationContext.ReportDiagnostic(operationContext.Operation.Syntax.CreateDiagnostic(ArgumentCountRule));
115139
}
116140
}, OperationKind.Invocation);
117141
});
@@ -308,7 +332,7 @@ private static int GetFormattingArguments(string format)
308332

309333
} // end of main loop
310334

311-
return uniqueNumbers.Count;
335+
return uniqueNumbers.Count == 0 ? 0 : uniqueNumbers.Max() + 1;
312336
}
313337

314338
private class StringFormatInfo
@@ -352,7 +376,7 @@ public StringFormatInfo(Compilation compilation)
352376
}
353377

354378
// Check if this the underlying method is user configured string formatting method.
355-
var additionalStringFormatMethodsOption = context.Options.GetAdditionalStringFormattingMethodsOption(Rule, context.Operation.Syntax.SyntaxTree, context.Compilation);
379+
var additionalStringFormatMethodsOption = context.Options.GetAdditionalStringFormattingMethodsOption(ArgumentCountRule, context.Operation.Syntax.SyntaxTree, context.Compilation);
356380
if (additionalStringFormatMethodsOption.Contains(method.OriginalDefinition) &&
357381
TryGetFormatInfoByParameterName(method, out info))
358382
{
@@ -362,7 +386,7 @@ public StringFormatInfo(Compilation compilation)
362386
// Check if the user configured automatic determination of formatting methods.
363387
// If so, check if the method called has a 'string format' parameter followed by an params array.
364388
var determineAdditionalStringFormattingMethodsAutomatically = context.Options.GetBoolOptionValue(EditorConfigOptionNames.TryDetermineAdditionalStringFormattingMethodsAutomatically,
365-
Rule, context.Operation.Syntax.SyntaxTree, context.Compilation, defaultValue: false);
389+
ArgumentCountRule, context.Operation.Syntax.SyntaxTree, context.Compilation, defaultValue: false);
366390
if (determineAdditionalStringFormattingMethodsAutomatically &&
367391
TryGetFormatInfoByParameterName(method, out info) &&
368392
info.ExpectedStringFormatArgumentCount == -1)
@@ -424,6 +448,13 @@ private bool TryGetFormatInfo(IMethodSymbol method, int formatIndex, [NotNullWhe
424448

425449
private static int GetExpectedNumberOfArguments(ImmutableArray<IParameterSymbol> parameters, int formatIndex)
426450
{
451+
if (formatIndex == parameters.Length - 1)
452+
{
453+
// format specification is the last parameter (e.g. CompositeFormat.Parse)
454+
// this is therefore not a formatting method.
455+
return -2;
456+
}
457+
427458
// check params
428459
IParameterSymbol nextParameter = parameters[formatIndex + 1];
429460
if (nextParameter.IsParams)

src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.cs.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2288,6 +2288,11 @@ Obecné přetypování (IL unbox.any) používané sekvencí vrácenou metodou E
22882288
<target state="translated">Argument formátu, který se předává do System.String.Format, neobsahuje položku formátování, která odpovídá jednotlivým argumentům objektů, nebo naopak.</target>
22892289
<note />
22902290
</trans-unit>
2291+
<trans-unit id="ProvideCorrectArgumentsToFormattingMethodsInvalidFormatMessage">
2292+
<source>The format argument is not a valid format string</source>
2293+
<target state="new">The format argument is not a valid format string</target>
2294+
<note />
2295+
</trans-unit>
22912296
<trans-unit id="ProvideCorrectArgumentsToFormattingMethodsMessage">
22922297
<source>Provide correct arguments to formatting methods</source>
22932298
<target state="translated">Poskytněte metodám formátování správné argumenty</target>

src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.de.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2288,6 +2288,11 @@ Erweiterungen und benutzerdefinierte Konvertierungen werden bei generischen Type
22882288
<target state="translated">Das an "System.String.Format" übergebene Formatargument enthält kein Formatelement, das den einzelnen Objektargumenten entspricht bzw. umgekehrt.</target>
22892289
<note />
22902290
</trans-unit>
2291+
<trans-unit id="ProvideCorrectArgumentsToFormattingMethodsInvalidFormatMessage">
2292+
<source>The format argument is not a valid format string</source>
2293+
<target state="new">The format argument is not a valid format string</target>
2294+
<note />
2295+
</trans-unit>
22912296
<trans-unit id="ProvideCorrectArgumentsToFormattingMethodsMessage">
22922297
<source>Provide correct arguments to formatting methods</source>
22932298
<target state="translated">Geeignete Argumente für Formatierungsmethoden angeben</target>

src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.es.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2288,6 +2288,11 @@ La ampliación y las conversiones definidas por el usuario no se admiten con tip
22882288
<target state="translated">El argumento de cadena format pasado a System.String.Format no contiene un elemento de formato que corresponda a cada argumento de objeto o viceversa.</target>
22892289
<note />
22902290
</trans-unit>
2291+
<trans-unit id="ProvideCorrectArgumentsToFormattingMethodsInvalidFormatMessage">
2292+
<source>The format argument is not a valid format string</source>
2293+
<target state="new">The format argument is not a valid format string</target>
2294+
<note />
2295+
</trans-unit>
22912296
<trans-unit id="ProvideCorrectArgumentsToFormattingMethodsMessage">
22922297
<source>Provide correct arguments to formatting methods</source>
22932298
<target state="translated">Proporcionar argumentos correctos para los métodos de formato</target>

src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.fr.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2288,6 +2288,11 @@ Les conversions étendues et définies par l’utilisateur ne sont pas prises en
22882288
<target state="translated">L'argument de mise en forme passé à System.String.Format ne contient aucun élément de mise en forme pour chaque argument d'objet correspondant, ou vice versa.</target>
22892289
<note />
22902290
</trans-unit>
2291+
<trans-unit id="ProvideCorrectArgumentsToFormattingMethodsInvalidFormatMessage">
2292+
<source>The format argument is not a valid format string</source>
2293+
<target state="new">The format argument is not a valid format string</target>
2294+
<note />
2295+
</trans-unit>
22912296
<trans-unit id="ProvideCorrectArgumentsToFormattingMethodsMessage">
22922297
<source>Provide correct arguments to formatting methods</source>
22932298
<target state="translated">Indiquer le nombre correct d'arguments dans les méthodes de mise en forme</target>

src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.it.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2288,6 +2288,11 @@ L'ampliamento e le conversioni definite dall'utente non sono supportate con tipi
22882288
<target state="translated">L'argomento format passato a System.String.Format non contiene un elemento di formato corrispondente a ogni argomento dell'oggetto o viceversa.</target>
22892289
<note />
22902290
</trans-unit>
2291+
<trans-unit id="ProvideCorrectArgumentsToFormattingMethodsInvalidFormatMessage">
2292+
<source>The format argument is not a valid format string</source>
2293+
<target state="new">The format argument is not a valid format string</target>
2294+
<note />
2295+
</trans-unit>
22912296
<trans-unit id="ProvideCorrectArgumentsToFormattingMethodsMessage">
22922297
<source>Provide correct arguments to formatting methods</source>
22932298
<target state="translated">Fornire gli argomenti corretti ai metodi di formattazione</target>

src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.ja.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2288,6 +2288,11 @@ Enumerable.OfType&lt;T&gt; で使用されるジェネリック型チェック (
22882288
<target state="translated">System.String.Format に渡される書式引数には、各オブジェクト引数に対応する書式項目が含まれていないか、各書式項目に対応するオブジェクト引数が含まれていません。</target>
22892289
<note />
22902290
</trans-unit>
2291+
<trans-unit id="ProvideCorrectArgumentsToFormattingMethodsInvalidFormatMessage">
2292+
<source>The format argument is not a valid format string</source>
2293+
<target state="new">The format argument is not a valid format string</target>
2294+
<note />
2295+
</trans-unit>
22912296
<trans-unit id="ProvideCorrectArgumentsToFormattingMethodsMessage">
22922297
<source>Provide correct arguments to formatting methods</source>
22932298
<target state="translated">書式設定メソッドに正しい引数を指定します</target>

src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.ko.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2288,6 +2288,11 @@ Enumerable.OfType&lt;T&gt;에서 사용하는 제네릭 형식 검사(C# 'is'
22882288
<target state="translated">System.String.Format으로 전달된 format 인수에 각 개체 인수에 해당하는 format 항목이 포함되지 않으며 그 반대의 경우도 마찬가지입니다.</target>
22892289
<note />
22902290
</trans-unit>
2291+
<trans-unit id="ProvideCorrectArgumentsToFormattingMethodsInvalidFormatMessage">
2292+
<source>The format argument is not a valid format string</source>
2293+
<target state="new">The format argument is not a valid format string</target>
2294+
<note />
2295+
</trans-unit>
22912296
<trans-unit id="ProvideCorrectArgumentsToFormattingMethodsMessage">
22922297
<source>Provide correct arguments to formatting methods</source>
22932298
<target state="translated">서식 지정 메서드에 올바른 인수를 제공하세요.</target>

src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.pl.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2288,6 +2288,11 @@ Konwersje poszerzane i zdefiniowane przez użytkownika nie są obsługiwane w pr
22882288
<target state="translated">Argument formatu przekazywany do metody System.String.Format nie zawiera elementu formatu odpowiadającego każdemu argumentowi obiektu lub odwrotnie.</target>
22892289
<note />
22902290
</trans-unit>
2291+
<trans-unit id="ProvideCorrectArgumentsToFormattingMethodsInvalidFormatMessage">
2292+
<source>The format argument is not a valid format string</source>
2293+
<target state="new">The format argument is not a valid format string</target>
2294+
<note />
2295+
</trans-unit>
22912296
<trans-unit id="ProvideCorrectArgumentsToFormattingMethodsMessage">
22922297
<source>Provide correct arguments to formatting methods</source>
22932298
<target state="translated">Określ poprawne argumenty dla metod formatujących</target>

0 commit comments

Comments
 (0)