Skip to content

Conversation

@GuillaumeGomez
Copy link
Member

Fixes #9278.

This is something that we need for cg_gcc (cc @antoyo). It's almost the same as #6674 (which I used as base).

changelog: Add new disallowed_fields lint

r? @samueltardieu

@rustbot rustbot added needs-fcp S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Dec 11, 2025
// Very round-about way to get the field `DefId` from the expr: first we get its
// parent `Ty`. Then we go through all its fields to find the one with the expected
// name and get the `DefId` from it.
if let Some(parent_ty) = cx.typeck_results().expr_ty_opt(e)
Copy link
Member Author

Choose a reason for hiding this comment

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

I'm really not sure this is the best way to get the DefId from Expr::Field but couldn't find another way. I'd love to know if there is one!

Copy link
Contributor

Choose a reason for hiding this comment

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

This needs to use the adjusted type.

.filter_by_name_unhygienic(name)
.find(|assoc_item| ns.matches(Some(assoc_item.namespace())))
.map(|assoc_item| assoc_item.def_id),
ItemKind::Struct(_, _, rustc_hir::VariantData::Struct { fields, .. }) => fields.iter().find_map(|field| {
Copy link
Member Author

Choose a reason for hiding this comment

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

Basically added support to look for fields in lookup_path (above in this file). it's used to check if the paths are valid in the config file.

So to link to a field (for example with struct X { y: u8 }: X::y.

@GuillaumeGomez
Copy link
Member Author

Finally updated all things that needed to be updated. :')

@Jarcho
Copy link
Contributor

Jarcho commented Dec 11, 2025

This doesn't prevent reading via destructuring. e.g. let Foo { x } = y

@samueltardieu
Copy link
Member

r? @Jarcho

@rustbot rustbot assigned Jarcho and unassigned samueltardieu Dec 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-fcp S-waiting-on-review Status: Awaiting review from the assignee but also interested parties

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add disallowed_fields lint

5 participants