diff --git a/ChangeLog.md b/ChangeLog.md index cd73a99478..0b154262fa 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added +- [CLI] Suppress error code from Roslynator when it detects issues in code but runs successfully ([PR](https://github.com/dotnet/roslynator/pull/1756) by @mdrybak) + ### Fixed - Fix analyzer [RCS1074](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1074) ([PR](https://github.com/dotnet/roslynator/pull/1768) by @cbersch) diff --git a/src/CommandLine/Commands/AnalyzeCommand.cs b/src/CommandLine/Commands/AnalyzeCommand.cs index ba5d22e824..4e7000f087 100644 --- a/src/CommandLine/Commands/AnalyzeCommand.cs +++ b/src/CommandLine/Commands/AnalyzeCommand.cs @@ -81,11 +81,12 @@ public override async Task ExecuteAsync(ProjectOrSolution results = await codeAnalyzer.AnalyzeSolutionAsync(solution, f => IsMatch(f), cancellationToken); } - return new AnalyzeCommandResult( - (results.Any(f => f.Diagnostics.Length > 0 || f.CompilerDiagnostics.Length > 0)) ? CommandStatus.NotSuccess : CommandStatus.Success, - results); + return new AnalyzeCommandResult(GetCommandStatus(Options, results), results); } + private static CommandStatus GetCommandStatus(AnalyzeCommandLineOptions options, ImmutableArray results) + => (options.ReturnSuccessOnDiagnostics || !results.Any(f => f.Diagnostics.Length > 0 || f.CompilerDiagnostics.Length > 0)) ? CommandStatus.Success : CommandStatus.NotSuccess; + protected override void ProcessResults(IList results) { IEnumerable analysisResults = results.SelectMany(f => f.AnalysisResults); diff --git a/src/CommandLine/Options/AnalyzeCommandLineOptions.cs b/src/CommandLine/Options/AnalyzeCommandLineOptions.cs index 039d418d66..1088721402 100644 --- a/src/CommandLine/Options/AnalyzeCommandLineOptions.cs +++ b/src/CommandLine/Options/AnalyzeCommandLineOptions.cs @@ -39,6 +39,11 @@ public class AnalyzeCommandLineOptions : AbstractAnalyzeCommandLineOptions HelpText = "Indicates whether suppressed diagnostics should be reported.")] public bool ReportSuppressedDiagnostics { get; set; } + [Option( + longName: "return-success-on-diagnostics", + HelpText = "Indicates whether to return exit code 0 on runs when diagnostics are reported.")] + public bool ReturnSuccessOnDiagnostics { get; set; } + internal bool ValidateOutputFormat() { return ParseHelpers.TryParseOutputFormat(OutputFormat);