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

don't lint on x = x + y inside an AddAssign impl #1318

Merged
merged 2 commits into from
Dec 19, 2016
Merged

don't lint on x = x + y inside an AddAssign impl #1318

merged 2 commits into from
Dec 19, 2016

Conversation

oli-obk
Copy link
Contributor

@oli-obk oli-obk commented Nov 2, 2016

fixes #1302

// check that we are not inside an `impl AssignOp` of this exact operation
let parent_fn = cx.tcx.map.get_parent(e.id);
let parent_impl = cx.tcx.map.get_parent(parent_fn);
if parent_impl.as_u32() != 0 {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

without this check, the get call in the next line will panic. All other functions on map will also panic, so there's no Option way to do this correctly. Should probably fix this in rustc...

Copy link
Member

Choose a reason for hiding this comment

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

That's kind of a feature, Option<NodeId> is not necessary as there is syntax::ast::DUMMY_NODE_ID. Yup, that's weak typing, but it's more memory efficient, and NodeIds are used everywhere.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

uuuhh... once we get panic! inside const fn, we can properly implement NonZero::new and then we can go mad on the microoptimizations (most relevant here: Option<NonZero<NodeId>> being the same size as NodeId).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried to compare parent_impl with DUMMY_NODE_ID, but DUMMY_NODE_ID is !0 and not 0 (see http://manishearth.github.io/rust-internals-docs/syntax/ast/constant.DUMMY_NODE_ID.html ).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, it's not a dummy id, it's the CRATE_NODE_ID, I pushed a fix. The reason for the check is that the crate root is not in the tcx.map, so tcx.map.get(CRATE_NODE_ID) will panic

// check that we are not inside an `impl AssignOp` of this exact operation
let parent_fn = cx.tcx.map.get_parent(e.id);
let parent_impl = cx.tcx.map.get_parent(parent_fn);
if parent_impl.as_u32() != 0 {
Copy link
Member

Choose a reason for hiding this comment

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

That's kind of a feature, Option<NodeId> is not necessary as there is syntax::ast::DUMMY_NODE_ID. Yup, that's weak typing, but it's more memory efficient, and NodeIds are used everywhere.

@oli-obk
Copy link
Contributor Author

oli-obk commented Dec 19, 2016

rebased with just tiny changes to address rustc changes

@oli-obk oli-obk merged commit f8349e1 into rust-lang:master Dec 19, 2016
@oli-obk oli-obk deleted the op_assign_false_positive branch December 19, 2016 11:31
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.

assign_op_pattern false positive for Assign trait impl
2 participants