Skip to content

Commit

Permalink
Encapsulate utility for filtering diagnostics
Browse files Browse the repository at this point in the history
  • Loading branch information
jjonescz committed Mar 27, 2024
1 parent 5ec2c2b commit 654d458
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
21 changes: 2 additions & 19 deletions src/Compilers/CSharp/Portable/Binder/Binder_Operators.cs
Original file line number Diff line number Diff line change
Expand Up @@ -648,25 +648,8 @@ resultOperatorKind is BinaryOperatorKind.ObjectEqual or BinaryOperatorKind.Objec

if (needsFilterDiagnostics)
{
Debug.Assert(conversionDiagnostics != diagnostics);
diagnostics.AddDependencies(conversionDiagnostics);

var sourceBag = conversionDiagnostics.DiagnosticBag;
Debug.Assert(sourceBag is not null);

if (!sourceBag.IsEmptyWithoutResolution)
{
foreach (var diagnostic in sourceBag.AsEnumerableWithoutResolution())
{
var code = diagnostic is DiagnosticWithInfo { HasLazyInfo: true, LazyInfo.Code: var lazyCode } ? lazyCode : diagnostic.Code;
if ((ErrorCode)code is not ErrorCode.WRN_ConvertingLock)
{
diagnostics.Add(diagnostic);
}
}
}

conversionDiagnostics.Free();
conversionDiagnostics.CopyFilteredToAndFree(diagnostics,
static code => code is not ErrorCode.WRN_ConvertingLock);
}

resultConstant = FoldBinaryOperator(node, resultOperatorKind, resultLeft, resultRight, resultType, diagnostics);
Expand Down
26 changes: 26 additions & 0 deletions src/Compilers/CSharp/Portable/Binder/BindingDiagnosticBag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using Microsoft.CodeAnalysis.CSharp.Symbols;
using Microsoft.CodeAnalysis.PooledObjects;

Expand Down Expand Up @@ -185,5 +186,30 @@ internal void Add(DiagnosticInfo? info, Location location)
DiagnosticBag?.Add(info, location);
}
}

internal void CopyFilteredToAndFree(BindingDiagnosticBag target, Func<ErrorCode, bool> predicate)
{
Debug.Assert(target != this);
target.AddDependencies(this);

var bag = DiagnosticBag;
Debug.Assert(bag is not null);

if (bag?.IsEmptyWithoutResolution == false)
{
foreach (var diagnostic in bag.AsEnumerableWithoutResolution())
{
var code = diagnostic is DiagnosticWithInfo { HasLazyInfo: true, LazyInfo.Code: var lazyCode }
? lazyCode
: diagnostic.Code;
if (predicate((ErrorCode)code))
{
target.Add(diagnostic);
}
}
}

Free();
}
}
}

0 comments on commit 654d458

Please sign in to comment.