Skip to content

Commit 222109c

Browse files
committed
Update SA1514 to observe (conversion) operator declarations
1 parent 67f2527 commit 222109c

2 files changed

Lines changed: 151 additions & 1 deletion

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/LayoutRules/SA1514UnitTests.cs

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,154 @@ public class TestClass
396396
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
397397
}
398398

399+
/// <summary>
400+
/// Verifies that operator declarations with valid (or no) documentation will not produce diagnostics.
401+
/// </summary>
402+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
403+
[Fact]
404+
public async Task TestValidOperatorDeclarationsAsync()
405+
{
406+
var testCode = @"namespace TestNamespace
407+
{
408+
public class TestClass
409+
{
410+
/// <summary>
411+
/// This is an operator.
412+
/// </summary>
413+
public static TestClass operator +(TestClass t1, TestClass t2)
414+
{
415+
return new TestClass();
416+
}
417+
418+
public int testField1;
419+
}
420+
}
421+
";
422+
423+
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
424+
}
425+
426+
/// <summary>
427+
/// Verifies that operator declarations with invalid documentation will produce the expected diagnostics.
428+
/// </summary>
429+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
430+
[Fact]
431+
public async Task TestInvalidOperatorDeclarationsAsync()
432+
{
433+
var testCode = @"namespace TestNamespace
434+
{
435+
public class TestClass
436+
{
437+
public int testField1;
438+
/// <summary>
439+
/// This is an operator.
440+
/// </summary>
441+
public static TestClass operator +(TestClass t1, TestClass t2)
442+
{
443+
return new TestClass();
444+
}
445+
}
446+
}
447+
";
448+
449+
var fixedTestCode = @"namespace TestNamespace
450+
{
451+
public class TestClass
452+
{
453+
public int testField1;
454+
455+
/// <summary>
456+
/// This is an operator.
457+
/// </summary>
458+
public static TestClass operator +(TestClass t1, TestClass t2)
459+
{
460+
return new TestClass();
461+
}
462+
}
463+
}
464+
";
465+
466+
var expectedDiagnostic = this.CSharpDiagnostic().WithLocation(6, 9);
467+
468+
await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostic, CancellationToken.None).ConfigureAwait(false);
469+
await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
470+
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
471+
}
472+
473+
/// <summary>
474+
/// Verifies that conversion operator declarations with valid (or no) documentation will not produce diagnostics.
475+
/// </summary>
476+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
477+
[Fact]
478+
public async Task TestValidConversionOperatorDeclarationsAsync()
479+
{
480+
var testCode = @"namespace TestNamespace
481+
{
482+
public class TestClass
483+
{
484+
/// <summary>
485+
/// This is a conversion operator.
486+
/// </summary>
487+
public static explicit operator TestClass(string foo)
488+
{
489+
return new TestClass();
490+
}
491+
492+
public int testField1;
493+
}
494+
}
495+
";
496+
497+
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
498+
}
499+
500+
/// <summary>
501+
/// Verifies that conversion operator declarations with invalid documentation will produce the expected diagnostics.
502+
/// </summary>
503+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
504+
[Fact]
505+
public async Task TestInvalidConversionOperatorDeclarationsAsync()
506+
{
507+
var testCode = @"namespace TestNamespace
508+
{
509+
public class TestClass
510+
{
511+
public int testField1;
512+
/// <summary>
513+
/// This is a conversion operator.
514+
/// </summary>
515+
public static explicit operator TestClass(string foo)
516+
{
517+
return new TestClass();
518+
}
519+
}
520+
}
521+
";
522+
523+
var fixedTestCode = @"namespace TestNamespace
524+
{
525+
public class TestClass
526+
{
527+
public int testField1;
528+
529+
/// <summary>
530+
/// This is a conversion operator.
531+
/// </summary>
532+
public static explicit operator TestClass(string foo)
533+
{
534+
return new TestClass();
535+
}
536+
}
537+
}
538+
";
539+
540+
var expectedDiagnostic = this.CSharpDiagnostic().WithLocation(6, 9);
541+
542+
await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostic, CancellationToken.None).ConfigureAwait(false);
543+
await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
544+
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
545+
}
546+
399547
/// <summary>
400548
/// Verifies that delegate declarations with valid (or no) documentation will not produce diagnostics.
401549
/// </summary>

StyleCop.Analyzers/StyleCop.Analyzers/LayoutRules/SA1514ElementDocumentationHeaderMustBePrecededByBlankLine.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,9 @@ internal class SA1514ElementDocumentationHeaderMustBePrecededByBlankLine : Diagn
8989
SyntaxKind.FieldDeclaration,
9090
SyntaxKind.DelegateDeclaration,
9191
SyntaxKind.EventDeclaration,
92-
SyntaxKind.EventFieldDeclaration);
92+
SyntaxKind.EventFieldDeclaration,
93+
SyntaxKind.OperatorDeclaration,
94+
SyntaxKind.ConversionOperatorDeclaration);
9395

9496
private static readonly Action<CompilationStartAnalysisContext> CompilationStartAction = HandleCompilationStart;
9597
private static readonly Action<SyntaxNodeAnalysisContext> DeclarationAction = HandleDeclaration;

0 commit comments

Comments
 (0)