Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 390b71d

Browse files
authored
Fix null check and parallel for each errors (#160)
* Fix unexpected null SymbolLocations. * Remove Parallel.ForEach. * Fix CA1860: Prefer comparing 'Count' to 0 rather than using 'Any()' both for clarity and for performance. --------- Co-authored-by: carlossanlop <[email protected]>
1 parent cc8e3e8 commit 390b71d

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

src/PortToTripleSlash/src/libraries/AllTypesVisitor.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Collections.Generic;
@@ -25,7 +25,13 @@ public override void VisitNamedType(INamedTypeSymbol symbol)
2525
Visit(typeMember);
2626
}
2727
}
28-
public override void VisitNamespace(INamespaceSymbol symbol) => Parallel.ForEach(symbol.GetMembers(), s => s.Accept(this));
28+
public override void VisitNamespace(INamespaceSymbol symbol)
29+
{
30+
foreach (INamespaceOrTypeSymbol s in symbol.GetMembers())
31+
{
32+
s.Accept(this);
33+
}
34+
}
2935
public override void VisitDynamicType(IDynamicTypeSymbol symbol) => AllTypesSymbols.Add(symbol);
3036
public override void VisitFunctionPointerType(IFunctionPointerTypeSymbol symbol) => AllTypesSymbols.Add(symbol);
3137
public override void VisitAlias(IAliasSymbol symbol) => AllTypesSymbols.Add(symbol);

src/PortToTripleSlash/src/libraries/ToTripleSlashPorter.cs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System;
@@ -91,7 +91,7 @@ public async Task StartAsync(CancellationToken cancellationToken)
9191
cancellationToken.ThrowIfCancellationRequested();
9292

9393
CollectFiles();
94-
if (!_docsComments.Types.Any())
94+
if (_docsComments.Types.Count == 0)
9595
{
9696
Log.Error("No docs files found.");
9797
return;
@@ -105,7 +105,7 @@ public async Task StartAsync(CancellationToken cancellationToken)
105105
/// </summary>
106106
public async Task MatchSymbolsAsync(Compilation compilation, bool isMSBuildProject, CancellationToken cancellationToken)
107107
{
108-
Debug.Assert(_docsComments.Types.Any());
108+
Debug.Assert(_docsComments.Types.Count != 0);
109109
cancellationToken.ThrowIfCancellationRequested();
110110

111111
Log.Info("Looking for symbol locations for all Docs types...");
@@ -127,13 +127,14 @@ public async Task MatchSymbolsAsync(Compilation compilation, bool isMSBuildProje
127127
/// </summary>
128128
public async Task PortAsync(bool isMSBuildProject, CancellationToken cancellationToken)
129129
{
130-
Debug.Assert(_docsComments.Types.Any());
130+
Debug.Assert(_docsComments.Types.Count != 0);
131131
cancellationToken.ThrowIfCancellationRequested();
132132

133133
Log.Info($"Now attempting to port all found symbols...");
134134
foreach (DocsType docsType in _docsComments.Types.Values)
135135
{
136-
if (docsType.SymbolLocations == null || !docsType.SymbolLocations.Any())
136+
Debug.Assert(docsType.SymbolLocations != null);
137+
if (docsType.SymbolLocations.Count == 0)
137138
{
138139
Log.Warning($"No symbols found for '{docsType.DocId}'. Skipping.");
139140
continue;
@@ -164,7 +165,11 @@ private static void CollectSymbolLocations(Compilation compilation, DocsType doc
164165
{
165166
FindLocationsOfSymbolInResolvedProject(docsType, compilation);
166167

167-
if (docsType.SymbolLocations == null || !docsType.SymbolLocations.Any())
168+
if (docsType.SymbolLocations == null)
169+
{
170+
throw new NullReferenceException();
171+
}
172+
if (docsType.SymbolLocations.Count == 0)
168173
{
169174
Log.Error($"No symbols found for docs type '{docsType.DocId}'.");
170175
}
@@ -245,16 +250,15 @@ private static void FindLocationsOfSymbolInResolvedProject(DocsType docsType, Co
245250
// Next, filter types that match the current docsType
246251
IEnumerable<ISymbol> currentTypeSymbols = visitor.AllTypesSymbols.Where(s => s != null && s.GetDocumentationCommentId() == docsType.DocId);
247252

253+
docsType.SymbolLocations ??= new();
248254
foreach (ISymbol symbol in currentTypeSymbols)
249255
{
250-
docsType.SymbolLocations = GetSymbolLocations(compilation, symbol);
256+
GetSymbolLocations(docsType.SymbolLocations, compilation, symbol);
251257
}
252258
}
253259

254-
private static List<ResolvedLocation> GetSymbolLocations(Compilation compilation, ISymbol symbol)
260+
private static void GetSymbolLocations(List<ResolvedLocation> resolvedLocations, Compilation compilation, ISymbol symbol)
255261
{
256-
List<ResolvedLocation> resolvedLocations = new();
257-
258262
int n = 0;
259263
string docId = symbol.GetDocumentationCommentId() ?? throw new NullReferenceException($"DocID was null for symbol '{symbol}'");
260264
foreach (Location location in symbol.Locations)
@@ -289,8 +293,6 @@ private static List<ResolvedLocation> GetSymbolLocations(Compilation compilation
289293
}
290294
n++;
291295
}
292-
293-
return resolvedLocations;
294296
}
295297

296298
[GeneratedRegex("src(?<separator>[\\\\\\/]{1})coreclr")]

0 commit comments

Comments
 (0)