@@ -51,7 +51,7 @@ impl LateLintPass<'_> for MyStructLint {
5151 fn check_expr (& mut self , cx : & LateContext <'_ >, expr : & Expr <'_ >) {
5252 // Get type of `expr`
5353 let ty = cx . typeck_results (). expr_ty (expr );
54-
54+
5555 // Check if the `Ty` of this expression is of character type
5656 if ty . is_char () {
5757 println! (" Our expression is a char!" );
@@ -70,18 +70,18 @@ pub fn is_char(self) -> bool {
7070}
7171```
7272
73- Indeed, we just discovered ` Ty ` 's [ ` kind ` method] [ kind ] , which provides us
73+ Indeed, we just discovered ` Ty ` 's [ ` kind() ` method] [ kind ] , which provides us
7474with [ ` TyKind ` ] [ TyKind ] of a ` Ty ` .
7575
7676## ` TyKind `
7777
7878` TyKind ` defines the kinds of types in Rust's type system.
7979Peeking into [ ` TyKind ` documentation] [ TyKind ] , we will see that it is an
80- enum of 27 variants, including items such as ` Bool ` , ` Int ` , ` Ref ` , etc.
80+ enum of over 25 variants, including items such as ` Bool ` , ` Int ` , ` Ref ` , etc.
8181
8282### ` kind ` Usage
8383
84- The ` TyKind ` of ` Ty ` can be returned by calling [ ` Ty.kind ` method] [ kind ] .
84+ The ` TyKind ` of ` Ty ` can be returned by calling [ ` Ty.kind() ` method] [ kind ] .
8585We often use this method to perform pattern matching in Clippy.
8686
8787For instance, if we want to check for a ` struct ` , we could examine if the
@@ -107,15 +107,21 @@ impl LateLintPass<'_> for MyStructLint {
107107We've been talking about [ ` ty::Ty ` ] [ middle_ty ] this whole time without addressing [ ` hir::Ty ` ] [ hir_ty ] , but the latter
108108is also important to understand.
109109
110- ` hir::Ty ` would represent * what* an user wrote, while ` ty::Ty ` would understand the meaning of it (because it has more
111- information).
110+ ` hir::Ty ` would represent * what* the user wrote, while ` ty::Ty ` is how the compiler sees the type and has more
111+ information. Example:
112112
113- ** Example: ` fn foo(x: u32) -> u32 { x } ` **
113+ ``` rust
114+ fn foo (x : u32 ) -> u32 { x }
115+ ```
114116
115117Here the HIR sees the types without "thinking" about them, it knows that the function takes an ` u32 ` and returns
116- an ` u32 ` . But at the ` ty::Ty ` level the compiler understands that they're the same type, in-depth lifetimes, etc...
118+ an ` u32 ` . As far as ` hir::Ty ` is concerned those might be different types. But at the ` ty::Ty ` level the compiler
119+ understands that they're the same type, in-depth lifetimes, etc...
120+
121+ To get from a ` hir::Ty ` to a ` ty::Ty ` , you can use the [ ` hir_ty_to_ty ` ] [ hir_ty_to_ty ] function outside of bodies or
122+ outside of bodies the [ ` TypeckResults::node_type() ` ] [ node_type ] method.
117123
118- you can use the [ ` hir_ty_to_ty ` ] [ hir_ty_to_ty ] function to convert from a ` hir::Ty ` to a ` ty::Ty `
124+ > ** Warning ** : Don't use ` hir_ty_to_ty ` inside of bodies, because this can cause ICEs.
119125
120126## Useful Links
121127
@@ -130,6 +136,7 @@ in this chapter:
130136[ Adt ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/enum.TyKind.html#variant.Adt
131137[ AdtDef ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/adt/struct.AdtDef.html
132138[ expr_ty ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.TypeckResults.html#method.expr_ty
139+ [ node_type ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.TypeckResults.html#method.node_type
133140[ is_char ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.Ty.html#method.is_char
134141[ is_char_source ] : https://doc.rust-lang.org/nightly/nightly-rustc/src/rustc_middle/ty/sty.rs.html#1831-1834
135142[ kind ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.Ty.html#method.kind
0 commit comments