Skip to content

Commit 01f822a

Browse files
Improvement
1 parent 3a83bd6 commit 01f822a

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

CheckedExceptions/CheckedExceptionsAnalyzer.Shared.cs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Microsoft.CodeAnalysis;
22
using Microsoft.CodeAnalysis.CSharp.Syntax;
3+
using Microsoft.CodeAnalysis.Text;
34

45
namespace Sundstrom.CheckedExceptions;
56

@@ -124,11 +125,20 @@ private IEnumerable<INamedTypeSymbol> GetExceptionTypes(AttributeSyntax attribut
124125
return node.Ancestors().OfType<CatchClauseSyntax>().FirstOrDefault();
125126
}
126127

127-
private static SyntaxNode GetSignificantNode(SyntaxNode expression)
128+
private static Location GetSignificantLocation(SyntaxNode expression)
129+
{
130+
if (expression is InvocationExpressionSyntax)
131+
return GetSignificantInvocationLocation(expression);
132+
133+
var node = GetSignificantNodeCore(expression);
134+
return node.GetLocation();
135+
}
136+
137+
private static SyntaxNode GetSignificantNodeCore(SyntaxNode expression)
128138
{
129139
if (expression is InvocationExpressionSyntax ie)
130140
{
131-
return GetSignificantNode(ie.Expression);
141+
return GetSignificantNodeCore(ie.Expression);
132142
}
133143

134144
if (expression is MemberAccessExpressionSyntax mae)
@@ -138,4 +148,22 @@ private static SyntaxNode GetSignificantNode(SyntaxNode expression)
138148

139149
return expression;
140150
}
151+
152+
private static Location GetSignificantInvocationLocation(SyntaxNode expression)
153+
{
154+
if (expression is InvocationExpressionSyntax invocation)
155+
{
156+
// Get the name part (e.g., bar in foo.bar(s))
157+
var nameNode = GetSignificantNodeCore(invocation.Expression);
158+
159+
// Compute the span from name start to the full invocation end
160+
var start = nameNode.SpanStart;
161+
var end = invocation.Span.End;
162+
163+
var span = TextSpan.FromBounds(start, end);
164+
return Location.Create(invocation.SyntaxTree, span);
165+
}
166+
167+
return expression.GetLocation();
168+
}
141169
}

CheckedExceptions/CheckedExceptionsAnalyzer.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ private void AnalyzeExceptionsInTryBlock(SyntaxNodeAnalysisContext context, TryS
351351
if (ShouldIgnore(throwStatement, mode))
352352
{
353353
// Report as THROW002 (Info level)
354-
var diagnostic = Diagnostic.Create(RuleIgnoredException, throwStatement.GetLocation(), exceptionType.Name);
354+
var diagnostic = Diagnostic.Create(RuleIgnoredException, GetSignificantLocation(throwStatement), exceptionType.Name);
355355
context.ReportDiagnostic(diagnostic);
356356
continue;
357357
}
@@ -368,7 +368,7 @@ private void AnalyzeExceptionsInTryBlock(SyntaxNodeAnalysisContext context, TryS
368368
// Report diagnostic for unhandled exception
369369
var diagnostic = Diagnostic.Create(
370370
RuleUnhandledException,
371-
throwStatement.GetLocation(),
371+
GetSignificantLocation(throwStatement),
372372
exceptionType.Name,
373373
THROW001Verbs.MightBe);
374374

@@ -1195,7 +1195,7 @@ private void AnalyzeExceptionThrowingNode(
11951195
if (ShouldIgnore(node, mode))
11961196
{
11971197
// Report as THROW002 (Info level)
1198-
var diagnostic = Diagnostic.Create(RuleIgnoredException, GetSignificantNode(node).GetLocation(), exceptionType.Name);
1198+
var diagnostic = Diagnostic.Create(RuleIgnoredException, GetSignificantLocation(node), exceptionType.Name);
11991199
context.ReportDiagnostic(diagnostic);
12001200
return;
12011201
}
@@ -1204,7 +1204,7 @@ private void AnalyzeExceptionThrowingNode(
12041204
// Check for general exceptions
12051205
if (IsGeneralException(exceptionType))
12061206
{
1207-
context.ReportDiagnostic(Diagnostic.Create(RuleGeneralThrow, GetSignificantNode(node).GetLocation()));
1207+
context.ReportDiagnostic(Diagnostic.Create(RuleGeneralThrow, GetSignificantLocation(node)));
12081208
}
12091209

12101210
// Check if the exception is declared via [Throws]
@@ -1223,7 +1223,7 @@ private void AnalyzeExceptionThrowingNode(
12231223

12241224
var verb = isThrowingConstruct ? THROW001Verbs.Is : THROW001Verbs.MightBe;
12251225

1226-
var diagnostic = Diagnostic.Create(RuleUnhandledException, GetSignificantNode(node).GetLocation(), properties, exceptionType.Name, verb);
1226+
var diagnostic = Diagnostic.Create(RuleUnhandledException, GetSignificantLocation(node), properties, exceptionType.Name, verb);
12271227
context.ReportDiagnostic(diagnostic);
12281228
}
12291229
}

0 commit comments

Comments
 (0)