Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Questions on null_check_on_nullable_type_parameter #3256

Open
srawlins opened this issue Feb 27, 2022 · 2 comments
Open

Questions on null_check_on_nullable_type_parameter #3256

srawlins opened this issue Feb 27, 2022 · 2 comments
Labels
false-negative P2 A bug or feature request we're likely to work on set-core Affects a rule in the core Dart rule set type-question A question about expected behavior or functionality

Comments

@srawlins
Copy link
Member

I was looking at test cases in order to implement a quick fix for null_check_on_nullable_type_parameter but came across some test cases that surprised me.

Future<T> / await p!:

Future<T> m40<T extends Object?>(T? p) async => await p!; // LINT

This case does not generate a lint report, but I expected it would.

List<T> / [p!]:

void ff<T>(T? p) {
  <T>[p!]; // LINT
}

This case also does not generate a lint report, but I expected it would.

Stream<T> / yield p!:

Stream<T> m41<T extends Object?>(T? p) async* { yield p!; }

This case also does not generate a lint report, but I expected it would.

@srawlins srawlins added the type-question A question about expected behavior or functionality label Feb 27, 2022
@srawlins
Copy link
Member Author

CC @leafpetersen who filed the initial lint rule request

CC @a14n who implemented the lint rule

CC @goderbauer in case this affects the decision to add this to lints/recommended.yaml as per dart-lang/lints#64

@a14n
Copy link
Contributor

a14n commented Feb 28, 2022

This case also does not generate a lint report, but I expected it would.

I expected the same. Those cases were just forgotten during the implementation. I guess the following method should be extended to cover those cases.

DartType? getExpectedType(PostfixExpression node) {
var realNode =
node.thisOrAncestorMatching((e) => e.parent is! ParenthesizedExpression);
var parent = realNode?.parent;
// in return value
if (parent is ReturnStatement || parent is ExpressionFunctionBody) {
var parentExpression = parent?.thisOrAncestorOfType<FunctionExpression>();
if (parentExpression == null) {
return null;
}
var staticType = parentExpression.staticType;
return staticType is FunctionType ? staticType.returnType : null;
}
// assignment
if (parent is AssignmentExpression &&
parent.operator.type == TokenType.EQ &&
(parent.leftHandSide is! Identifier ||
node.operand is! Identifier ||
(parent.leftHandSide as Identifier).name !=
(node.operand as Identifier).name)) {
return parent.writeType;
}
// in variable declaration
if (parent is VariableDeclaration) {
return parent.declaredElement?.type;
}
// as right member of binary operator
if (parent is BinaryExpression && parent.rightOperand == realNode) {
var parentElement = parent.staticElement;
if (parentElement == null) {
return null;
}
return parentElement.parameters.first.type;
}
// as parameter of function
if (parent is NamedExpression) {
realNode = parent;
parent = parent.parent;
}
if (parent is ArgumentList && realNode is Expression) {
return realNode.staticParameterElement?.type;
}
return null;
}
.

@pq pq added the set-core Affects a rule in the core Dart rule set label Jul 18, 2022
@srawlins srawlins added P2 A bug or feature request we're likely to work on false-negative and removed type-question A question about expected behavior or functionality labels Oct 26, 2022
@srawlins srawlins added the type-question A question about expected behavior or functionality label Mar 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
false-negative P2 A bug or feature request we're likely to work on set-core Affects a rule in the core Dart rule set type-question A question about expected behavior or functionality
Projects
None yet
Development

No branches or pull requests

3 participants