Skip to content

Commit

Permalink
[element model] migrate unnecessary_this
Browse files Browse the repository at this point in the history
Bug: https://github.com/dart-lang/linter/issues/5099
Change-Id: I46c62d689518c1091d4aa9262b6939948f6be515
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/394562
Auto-Submit: Phil Quitslund <[email protected]>
Commit-Queue: Brian Wilkerson <[email protected]>
Commit-Queue: Phil Quitslund <[email protected]>
Reviewed-by: Brian Wilkerson <[email protected]>
  • Loading branch information
pq authored and Commit Queue committed Nov 11, 2024
1 parent 0b41ef1 commit e0e4b41
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 19 deletions.
3 changes: 0 additions & 3 deletions pkg/linter/analyzer_use_new_elements.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
lib/src/ast.dart
lib/src/extensions.dart
lib/src/rules/analyzer_use_new_elements.dart
lib/src/rules/avoid_renaming_method_parameters.dart
lib/src/rules/avoid_setters_without_getters.dart
lib/src/rules/avoid_types_as_parameter_names.dart
lib/src/rules/avoid_void_async.dart
lib/src/rules/deprecated_member_use_from_same_package.dart
lib/src/rules/invalid_runtime_check_with_js_interop_types.dart
Expand All @@ -12,7 +10,6 @@ lib/src/rules/prefer_final_fields.dart
lib/src/rules/prefer_initializing_formals.dart
lib/src/rules/public_member_api_docs.dart
lib/src/rules/unnecessary_overrides.dart
lib/src/rules/unnecessary_this.dart
lib/src/rules/use_build_context_synchronously.dart
lib/src/rules/use_key_in_widget_constructors.dart
lib/src/rules/use_late_for_private_fields_and_variables.dart
Expand Down
30 changes: 14 additions & 16 deletions pkg/linter/lib/src/rules/unnecessary_this.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/visitor.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/element2.dart';

import '../analyzer.dart';
import '../ast.dart';
Expand Down Expand Up @@ -49,11 +49,11 @@ class _Visitor extends SimpleAstVisitor<void> {
void visitThisExpression(ThisExpression node) {
var parent = node.parent;

Element? element;
Element2? element;
if (parent is PropertyAccess && !parent.isNullAware) {
element = getWriteOrReadElement(parent.propertyName);
element = getWriteOrReadElement2(parent.propertyName);
} else if (parent is MethodInvocation && !parent.isNullAware) {
element = parent.methodName.staticElement;
element = parent.methodName.element;
} else {
return;
}
Expand All @@ -63,25 +63,23 @@ class _Visitor extends SimpleAstVisitor<void> {
}
}

bool _canReferenceElementWithoutThisPrefix(Element? element, AstNode node) {
if (element == null) {
return false;
}
bool _canReferenceElementWithoutThisPrefix(Element2? element, AstNode node) {
if (element == null) return false;

var id = element.displayName;
var isSetter = element is PropertyAccessorElement && element.isSetter;
var result = resolveNameInScope(id, node, shouldResolveSetter: isSetter);
var result = resolveNameInScope(id, node,
shouldResolveSetter: element is SetterElement);

// No result, definitely no shadowing.
// The requested element is inherited, or from an extension.
if (result.isNone) {
return true;
}
if (result.isNone) return true;

var resultElement = result.element2;

// The result has the matching name, might be shadowing.
// Check that the element is the same.
if (result.isRequestedName) {
return result.element == element;
return resultElement == element;
}

// The result has the same basename, but not the same name.
Expand All @@ -90,8 +88,8 @@ class _Visitor extends SimpleAstVisitor<void> {
// - prevents us from going up to the library scope;
// - the requested element must be inherited, or from an extension.
if (result.isDifferentName) {
var enclosing = result.element?.enclosingElement3;
return enclosing is ClassElement;
var enclosing = resultElement?.enclosingElement2;
return enclosing is ClassElement2;
}

// Should not happen.
Expand Down

0 comments on commit e0e4b41

Please sign in to comment.