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

proposal: private_field_outside_class #4983

Closed
nate-thegrate opened this issue May 28, 2024 · 2 comments
Closed

proposal: private_field_outside_class #4983

nate-thegrate opened this issue May 28, 2024 · 2 comments
Labels
lint-proposal status-pending type-enhancement A request for a change that isn't a bug

Comments

@nate-thegrate
Copy link

nate-thegrate commented May 28, 2024

Description

Avoid referring to a private field of a class outside the class declaration.

Can be fixed by making the field public, making the class private, making the enclosing reference private, adding the base/sealed/final modifier, or moving the reference into the class declaration.

Kind: AVOID

Currently, the following produces zero static analysis warnings:

interface class A {
  final int _value = 0;
}

int getValue(A a) => a._value; // always throws an error

And when combined with an abstract_private_class_member violation, it doesn't matter whether the class is extended or implemented:

abstract class A {
  int get _value;
}

int getValue(A a) => a._value; // always throws an error

Examples

class A {
  final int _value = 0;
}

// BAD
int getValue(A a) => a._value;

class B {
  // BAD
  factory B.fromA(A a) => B(a._value);
}


// GOOD, class is private
class _A {
  final int _value = 0;
}

// GOOD, enclosing function is private
int _getValue(A a) => a._value;

// GOOD, field is public
class A {
  final int value = 0;
}

// GOOD, class has the "base" modifier
base class A {
  final int _value = 0;
}

// GOOD, private field is accessed within the class
class A {
  final int _value = 0;
  int get value => _value;
}

Discussion

There probably wouldn't be a "quick-fix" for the rule, since there are many solutions (some of which may cause a name overlap).

Probably low-priority

If this proposal is motivated by real-world examples, please provide as many details as you can. Demonstrating potential impact is especially valuable.

I've never seen a real-world situation where this rule has been broken.

@srawlins
Copy link
Member

See also dart-lang/sdk#51641, a very similar request.

@nate-thegrate
Copy link
Author

@srawlins good to know, thanks!

I'm good with closing this one as a duplicate.

@nate-thegrate nate-thegrate closed this as not planned Won't fix, can't repro, duplicate, stale Jul 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lint-proposal status-pending type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

2 participants