Skip to content

Commit a0971c9

Browse files
authored
Merge pull request #2889 from sharwell/fix-missing-select
Fix handling of missing 'select' clause
2 parents dc664d9 + 004a249 commit a0971c9

3 files changed

Lines changed: 97 additions & 3 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1103CodeFixProvider.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,11 @@ private static ImmutableArray<SyntaxNode> CreateQueryNodeList(QueryExpressionSyn
153153
private static void ProcessQueryBody(QueryBodySyntax body, List<SyntaxNode> queryNodes)
154154
{
155155
queryNodes.AddRange(body.Clauses);
156-
queryNodes.Add(body.SelectOrGroup);
156+
157+
if (!body.SelectOrGroup.IsMissing)
158+
{
159+
queryNodes.Add(body.SelectOrGroup);
160+
}
157161

158162
if (body.Continuation != null)
159163
{

StyleCop.Analyzers/StyleCop.Analyzers.Test/ReadabilityRules/SA1103UnitTests.cs

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ namespace StyleCop.Analyzers.Test.ReadabilityRules
55
{
66
using System.Threading;
77
using System.Threading.Tasks;
8+
using Microsoft.CodeAnalysis;
89
using Microsoft.CodeAnalysis.Testing;
910
using StyleCop.Analyzers.ReadabilityRules;
10-
using TestHelper;
1111
using Xunit;
1212
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
1313
StyleCop.Analyzers.ReadabilityRules.SA110xQueryClauses,
@@ -365,5 +365,91 @@ public void TestMethod()
365365

366366
await VerifyCSharpFixAsync(testCode, expectedDiagnostics, fixedTestCode, CancellationToken.None).ConfigureAwait(false);
367367
}
368+
369+
[Fact]
370+
[WorkItem(2888, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2888")]
371+
public async Task TestQueryExpressionWithMissingSelectAsync()
372+
{
373+
var testCode = @"namespace TestNamespace
374+
{
375+
using System.Linq;
376+
377+
public class TestClass
378+
{
379+
private int[] testArray = { 1, 2, 3, 4, 5 };
380+
381+
public void TestMethod()
382+
{
383+
var x = from element in testArray
384+
where element > 1;
385+
}
386+
}
387+
}
388+
";
389+
390+
var expectedDiagnostics = new DiagnosticResult("CS0742", DiagnosticSeverity.Error).WithLocation(12, 38).WithMessage("A query body must end with a select clause or a group clause");
391+
await VerifyCSharpFixAsync(testCode, expectedDiagnostics, testCode, CancellationToken.None).ConfigureAwait(false);
392+
}
393+
394+
[Fact]
395+
[WorkItem(2888, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2888")]
396+
public async Task TestQueryExpressionWithMissingSelect2Async()
397+
{
398+
var testCode = @"namespace TestNamespace
399+
{
400+
using System.Linq;
401+
402+
public class TestClass
403+
{
404+
private int[] testArray = { 1, 2, 3, 4, 5 };
405+
406+
public void TestMethod()
407+
{
408+
var x = from element in testArray where element < 3
409+
where element > 1;
410+
}
411+
}
412+
}
413+
";
414+
var fixedTestCode = @"namespace TestNamespace
415+
{
416+
using System.Linq;
417+
418+
public class TestClass
419+
{
420+
private int[] testArray = { 1, 2, 3, 4, 5 };
421+
422+
public void TestMethod()
423+
{
424+
var x = from element in testArray
425+
where element < 3
426+
where element > 1;
427+
}
428+
}
429+
}
430+
";
431+
432+
await new CSharpTest
433+
{
434+
TestState =
435+
{
436+
Sources = { testCode },
437+
ExpectedDiagnostics =
438+
{
439+
Diagnostic(SA110xQueryClauses.SA1103Descriptor).WithLocation(11, 21),
440+
new DiagnosticResult("CS0742", DiagnosticSeverity.Error).WithLocation(12, 38).WithMessage("A query body must end with a select clause or a group clause"),
441+
},
442+
},
443+
FixedState =
444+
{
445+
Sources = { fixedTestCode },
446+
ExpectedDiagnostics =
447+
{
448+
new DiagnosticResult("CS0742", DiagnosticSeverity.Error).WithLocation(13, 38).WithMessage("A query body must end with a select clause or a group clause"),
449+
},
450+
},
451+
CodeFixIndex = 1,
452+
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
453+
}
368454
}
369455
}

StyleCop.Analyzers/StyleCop.Analyzers/ReadabilityRules/SA110xQueryClauses.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,11 @@ private static void HandleSelectOrGroup(SelectOrGroupClauseSyntax selectOrGroup,
218218
{
219219
case SyntaxKind.SelectClause:
220220
var selectClause = (SelectClauseSyntax)selectOrGroup;
221-
tokensToCheck.Add(selectClause.SelectKeyword);
221+
if (!selectClause.IsMissing)
222+
{
223+
tokensToCheck.Add(selectClause.SelectKeyword);
224+
}
225+
222226
break;
223227
case SyntaxKind.GroupClause:
224228
var groupClause = (GroupClauseSyntax)selectOrGroup;

0 commit comments

Comments
 (0)