Repository: SeaQL/sea-orm (Rust)

Issue:
scripts generated by `sea-orm-cli` cannot pass clippy 
This is typical script generated by `sea-orm-cli` without existing relation:
```rust
//! SeaORM Entity. Generated by sea-orm-codegen 0.3.2

use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(table_name = "posts")]
pub struct Model {
    #[sea_orm(primary_key)]
    #[serde(skip_deserializing)]
    pub id: i32,
    pub title: String,
    #[sea_orm(column_type = "Text")]
    pub text: String,
}

#[derive(Copy, Clone, Debug, EnumIter)]
pub enum Relation {}

impl RelationTrait for Relation {
    fn def(&self) -> RelationDef {
        match self {
            _ => panic!("No RelationDef"),
        }
    }
}

impl ActiveModelBehavior for ActiveModel {}
```

When I run `cargo cilppy`, there is a warning:
```
warning: this match could be replaced by its body itself
  --> src/post.rs:22:9
   |
22 | /         match self {
23 | |             _ => panic!("No RelationDef"),
24 | |         }
   | |_________^ help: consider using the match body instead: `panic!("No RelationDef")`
   |
   = note: `#[warn(clippy::match_single_binding)]` on by default
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#match_single_binding
``` 
Could someone fix it?


Discussion:
Hi @YoshieraHuang, thanks for pointing that out! We could fix it for sure.
Hi, it seems that you are not using `DeriveRelation`, it shouldn't warn when it is expanded inside the derive macro.

Or is it really how we generate the entity for now? @billy1624 
> Or is it really how we generate the entity for now? @billy1624

Yes! It's generated by codegen not derived by derive macros
> Yes! It's generated by codegen not derived by derive macros

Oh, the following shouldn't warn:

```rust
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}

// no need for impl RelationTrait for Relation
```
> > Yes! It's generated by codegen not derived by derive macros
> 
> Oh, the following shouldn't warn:
> 
> ```rust
> #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
> pub enum Relation {}
> 
> // no need for impl RelationTrait for Relation
> ```

Correct, but the point is codegen with `expanded` flag will generate the following...

```rust
#[derive(Copy, Clone, Debug, EnumIter)]
pub enum Relation {}

impl RelationTrait for Relation {
    fn def(&self) -> RelationDef {
        match self {
            _ => panic!("No RelationDef"),
        }
    }
}
```

I will fix it!



Patch:
diff --git a/sea-orm-codegen/src/entity/writer.rs b/sea-orm-codegen/src/entity/writer.rs
index be3c47bbed..0efba30647 100644
--- a/sea-orm-codegen/src/entity/writer.rs
+++ b/sea-orm-codegen/src/entity/writer.rs
@@ -328,19 +328,19 @@ impl EntityWriter {
         let relation_defs = entity.get_relation_defs();
         let quoted = if relation_ref_tables_camel_case.is_empty() {
             quote! {
-                _ => panic!("No RelationDef"),
+                panic!("No RelationDef")
             }
         } else {
             quote! {
-                #(Self::#relation_ref_tables_camel_case => #relation_defs,)*
+                match self {
+                    #(Self::#relation_ref_tables_camel_case => #relation_defs,)*
+                }
             }
         };
         quote! {
             impl RelationTrait for Relation {
                 fn def(&self) -> RelationDef {
-                    match self {
-                        #quoted
-                    }
+                    #quoted
                 }
             }
         }


Test patch:
diff --git a/sea-orm-codegen/tests/compact/filling.rs b/sea-orm-codegen/tests/compact/filling.rs
index 820f65c85a..dfedb1a775 100644
--- a/sea-orm-codegen/tests/compact/filling.rs
+++ b/sea-orm-codegen/tests/compact/filling.rs
@@ -15,9 +15,7 @@ pub enum Relation {}
 
 impl RelationTrait for Relation {
     fn def(&self) -> RelationDef {
-        match self {
-            _ => panic!("No RelationDef"),
-        }
+        panic!("No RelationDef")
     }
 }
 
diff --git a/sea-orm-codegen/tests/compact/rust_keyword.rs b/sea-orm-codegen/tests/compact/rust_keyword.rs
index 229eae221a..9a7bd9eb3d 100644
--- a/sea-orm-codegen/tests/compact/rust_keyword.rs
+++ b/sea-orm-codegen/tests/compact/rust_keyword.rs
@@ -21,9 +21,7 @@ pub enum Relation {}
 
 impl RelationTrait for Relation {
     fn def(&self) -> RelationDef {
-        match self {
-            _ => panic!("No RelationDef"),
-        }
+        panic!("No RelationDef")
     }
 }
 
diff --git a/sea-orm-codegen/tests/expanded/filling.rs b/sea-orm-codegen/tests/expanded/filling.rs
index 73d58152c5..76764ecf23 100644
--- a/sea-orm-codegen/tests/expanded/filling.rs
+++ b/sea-orm-codegen/tests/expanded/filling.rs
@@ -51,9 +51,7 @@ impl ColumnTrait for Column {
 
 impl RelationTrait for Relation {
     fn def(&self) -> RelationDef {
-        match self {
-            _ => panic!("No RelationDef"),
-        }
+        panic!("No RelationDef")
     }
 }
 
diff --git a/sea-orm-codegen/tests/expanded/rust_keyword.rs b/sea-orm-codegen/tests/expanded/rust_keyword.rs
index 1ab8a6275d..e83cc9ea1b 100644
--- a/sea-orm-codegen/tests/expanded/rust_keyword.rs
+++ b/sea-orm-codegen/tests/expanded/rust_keyword.rs
@@ -70,9 +70,7 @@ impl ColumnTrait for Column {
 
 impl RelationTrait for Relation {
     fn def(&self) -> RelationDef {
-        match self {
-            _ => panic!("No RelationDef"),
-        }
+        panic!("No RelationDef")
     }
 }