11

Roslyn

 4 years ago
source link: https://hownot2code.com/2020/10/30/roslyn/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

How Not To Code

C, C++, C#, Java bad practices: learn how to make a good code by bad example

Skip to content

Search
689dc9661e2a4a3e8143a85a5b843a4b.png
public SyntaxToken GenerateUniqueName(SemanticModel semanticModel, 
                                      SyntaxNode location, 
                                      SyntaxNode containerOpt, 
                                      string baseName, 
                                      CancellationToken cancellationToken)
{
  return GenerateUniqueName(semanticModel, 
                            location, 
                            containerOpt, 
                            baseName, 
                            filter: null, 
                            usedNames: null,    
                            cancellationToken);
}

V3156 The sixth argument of the ‘GenerateUniqueName’ method is passed as an argument to the ‘Concat’ method and is not expected to be null. Potential null value: null. AbstractSemanticFactsService.cs 24

We’ll be honest: when making this diagnostic, we didn’t really expect triggering warnings for simple null. After all, it is quite strange to pass null to a method that throws an exception because of it. Although, we have seen places where this was justified (for example, with the Expression class), but that’s not the point now.

So, we were very intrigued when we saw this warning. Let’s see what is happening in the GenerateUniqueName method.

public SyntaxToken GenerateUniqueName(SemanticModel semanticModel,
                                      SyntaxNode location, 
                                      SyntaxNode containerOpt,
                                      string baseName, 
                                      Func filter,
                                      IEnumerable usedNames, 
                                      CancellationToken cancellationToken)
{
  var container = containerOpt ?? location
                       .AncestorsAndSelf()
                       .FirstOrDefault(a => SyntaxFacts.IsExecutableBlock(a) 
                                         || SyntaxFacts.IsMethodBody(a));

  var candidates = GetCollidableSymbols(semanticModel, 
                                        location, 
                                        container, 
                                        cancellationToken);

  var filteredCandidates = filter != null ? candidates.Where(filter) 
                                          : candidates;

  return GenerateUniqueName(baseName, 
                            filteredCandidates.Select(s => s.Name)
                                              .Concat(usedNames));
}

As we can see, there is only one exit point in the method, no exceptions are thrown and there is no goto. In other words, nothing prevents us from passing usedNames to the Concat method and getting the ArgumentNullException.

Please click here to see more bugs from this project.


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK