Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(28)

Unified Diff: pkg/analysis_server/lib/src/services/search/search_engine.dart

Issue 2409403002: Issue 27542. Guard against CompilationUnitElement is null in SearchMatch.element getter. (Closed)
Patch Set: Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: pkg/analysis_server/lib/src/services/search/search_engine.dart
diff --git a/pkg/analysis_server/lib/src/services/search/search_engine.dart b/pkg/analysis_server/lib/src/services/search/search_engine.dart
index 72ca48283123d54ae5faf6f6d77e5f97133adb4e..e9c681d3c3fd945c461d23716d40e4a174aeaf55 100644
--- a/pkg/analysis_server/lib/src/services/search/search_engine.dart
+++ b/pkg/analysis_server/lib/src/services/search/search_engine.dart
@@ -155,16 +155,20 @@ class SearchMatch {
this.sourceRange, this.isResolved, this.isQualified);
/**
- * Return the [Element] containing the match.
+ * Return the [Element] containing the match. Can return `null` if the unit
+ * does not exist, or its element was invalidated, or the element cannot be
+ * found, etc.
*/
Element get element {
if (_element == null) {
CompilationUnitElement unitElement =
context.getCompilationUnitElement(unitSource, librarySource);
- _ContainingElementFinder finder =
- new _ContainingElementFinder(sourceRange.offset);
- unitElement.accept(finder);
- _element = finder.containingElement;
+ if (unitElement != null) {
+ _ContainingElementFinder finder =
+ new _ContainingElementFinder(sourceRange.offset);
+ unitElement.accept(finder);
+ _element = finder.containingElement;
+ }
}
return _element;
}
@@ -237,6 +241,16 @@ class SearchMatch {
buffer.write(")");
return buffer.toString();
}
+
+ /**
+ * Return elements of [matches] which has not-null elements.
+ *
+ * When [SearchMatch.element] is not `null` we cache its value, so it cannot
+ * become `null` later.
+ */
+ static List<SearchMatch> withNotNullElement(List<SearchMatch> matches) {
+ return matches.where((match) => match.element != null).toList();
+ }
}
/**

Powered by Google App Engine
This is Rietveld 408576698