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

Allow overloading of operators in unnecessary_statements #4469

Conversation

gaetschwartz
Copy link

Description

This PR disables the unnecessary_statements for overloaded operators, so that code like this is possible:

class Logger {
	void operator<<(String message) {/*...*/}
}

void main() {
	final l = Logger();

	l << "Hello world (づ。◕‿‿◕。)づ"; 
	// Unnecessary statement.
	// Try completing the statement or breaking it up. (unnecessary_statements)
}

Fixes #4468

@coveralls
Copy link

Coverage Status

coverage: 96.812% (+0.02%) from 96.796% when pulling 0183c4d on gaetschwartz:unnecessary_statements_operator_overloading into eace82e on dart-lang:main.

@@ -92,6 +94,48 @@ class _ReportNoClearEffectVisitor extends UnifyingAstVisitor {

@override
void visitBinaryExpression(BinaryExpression node) {
// look for the class of the left operand and its extensions
var clazz = context.currentUnit.unit.declarations
.whereType<ClassDeclaration>()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leaving open whether we agree that we want to loosen this check (which we should settle in the associated issue), I will note that if we do, we should try and optimized these new bits.

Here's a benchmark run before this change:

image

And after:

image

Either way, it does seem likely that we can establish if an operator is overriding more directly. Maybe UnnecessaryOverrides can provide some ideas? (In particular, some variation of enclosingElement.thisType.lookUpMethod2(...)?)

@srawlins
Copy link
Member

I don't see us accepting this, as there we are not treating #4468 as a valid issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

unnecessary_statements shouldn't trigger when the operator is overloaded
4 participants