about summary refs log tree commit diff
path: root/clippy_utils
diff options
context:
space:
mode:
authorJason Newcomb <jsnewcomb@pm.me>2024-08-03 00:29:23 -0400
committerJason Newcomb <jsnewcomb@pm.me>2025-05-16 06:08:35 -0400
commit57782e0ff1e9833e121187f1c33b72bec6a8a61a (patch)
tree8d05a5d3f4678f6635191403c2dcf414ceadf14e /clippy_utils
parent95d7eda0b44cc883516b9198dce3b05db3a581f9 (diff)
downloadrust-57782e0ff1e9833e121187f1c33b72bec6a8a61a.tar.gz
rust-57782e0ff1e9833e121187f1c33b72bec6a8a61a.zip
Rewrite `non_copy_const`
Diffstat (limited to 'clippy_utils')
-rw-r--r--clippy_utils/src/ty/mod.rs11
1 files changed, 11 insertions, 0 deletions
diff --git a/clippy_utils/src/ty/mod.rs b/clippy_utils/src/ty/mod.rs
index 26d41cfb497..c50ad17bfad 100644
--- a/clippy_utils/src/ty/mod.rs
+++ b/clippy_utils/src/ty/mod.rs
@@ -1361,3 +1361,14 @@ pub fn is_slice_like<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool {
         || ty.is_array()
         || matches!(ty.kind(), ty::Adt(adt_def, _) if cx.tcx.is_diagnostic_item(sym::Vec, adt_def.did()))
 }
+
+/// Gets the index of a field by name.
+pub fn get_field_idx_by_name(ty: Ty<'_>, name: Symbol) -> Option<usize> {
+    match *ty.kind() {
+        ty::Adt(def, _) if def.is_union() || def.is_struct() => {
+            def.non_enum_variant().fields.iter().position(|f| f.name == name)
+        },
+        ty::Tuple(_) => name.as_str().parse::<usize>().ok(),
+        _ => None,
+    }
+}