Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign upSee if we can catch common errors with a lint #573
Comments
This comment has been minimized.
|
Yeah, having lints for that would be awesome, even if to just add notes on Rust errors. Maybe we can give users some suggestions on how to make their code cleaner as well (though that is by far not as important as actually catching and explaining errors).
IIRC, clippy (cc @llogiq) contains a lint to catch invalid regexes, maybe they have a story for supporting other external crates? (Though regex is, to me, "practically in std"!)
… Am 12.01.2017 um 15:16 schrieb Sean Griffin ***@***.***>:
Some of the most common issues that people run into are:
• Column order doesn't match struct field order
• Column type doesn't match struct field type
• Struct doesn't have a field for all the selected columns
I'd eventually like to allow the order to differ from the table definition, but I'm not confident that we will ever be able to do that. While I do want to make sure that the errors from the compiler alone give at least enough information for experienced users to figure out what's going wrong, we can do better. I've often thought about trying to do more validation inside of #[derive(Queryable)]. However, the issue there is that we can't get information about the code other than the item being annotated, meaning we can't ask things about the table mod. We also have no database connection, so we can't go to the schema to ask questions either.
One issue I have with just validating from that derive though is that it assumes:
• The user is using infer_schema! to define that table
• The struct is meant to be one-to-one with a table that has the same name
• The select clause used to construct this will contain all the columns from a given table
And I've tried to avoid all of those assumptions in the design. So I think a lint makes a lot more sense, as it has access to way more information (meaning we don't need a database connection), and they can be much more granularly allowed by the user.
I'm unsure if these lints would be able to occur early enough to catch the most common cases, but we should definitely explore this option. Ideally I'd like to avoid using a database connection for this, and have it only affect types which implement HasTable, find the module based on the value of that associated type, and base the linting off the value of AllColumns and SqlType. I'd like to try and provide a specific error message with suggestions on how to correct for all of the following, probably in this order:
• struct order doesn't match column order (this one should only fail if all the field names match a column name, and the number of fields matches the number of columns)
• struct does not have a field for each column
• two or more struct fields have the same name as a column but appear in swapped positions
• field type doesn't match column type (error message should ideally include which Rust types could map to the column type, or what SQL type they could change their column to match the rust type)
One other slightly less common mistake I've seen people make, which we could definitely catch with a lint is people thinking that they should be putting diesel::types::Timestamp on their struct directly.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or mute the thread.
|
This comment has been minimized.
|
It looks like |
This comment has been minimized.
llogiq
commented
Jan 12, 2017
|
Feel free to open clippy issues with suggestions. I personally don't know enough about the inner workings of Diesel, so I don't know how to typecheck the schema, but I think at least linting fields of a fixed type within a struct is definitively easy. |
This comment has been minimized.
|
Orthogonal to lints, but related to nicer errors: https://scribbles.pascalhertleif.de/hook-into-rustc-errors.html |
killercup
added
the
discussion desired
label
Feb 17, 2017
killercup
referenced this issue
Apr 25, 2017
Closed
select query loads data incorrectly if model is 'out of order' #421
This comment has been minimized.
|
Additional case we should lint for: References on |
This comment has been minimized.
|
I'm going to stick this on the milestone. I think this would be a great thing to have done for 1.0. Right now I don't think that'll be possible, but if we have some more people come in to help with documentation I should have some more bandwidth to work on this. |
sgrif
added this to the 1.0 milestone
Aug 4, 2017
This comment has been minimized.
llogiq
commented
Aug 5, 2017
|
Ok I get this right: We want to lint structs that have a The problem here is that the error message is likely emitted (and compilation stopped) before lints run, so there's not much we can do. |
sgrif
referenced this issue
Sep 28, 2017
Closed
Can we emit compile-error on `.select(fieldname1)` for `struct{fieldname2)`? #1216
sgrif
removed this from the 1.0 milestone
Dec 2, 2017
This comment has been minimized.
|
This isn't happening in time for 1.0. |
sgrif commentedJan 12, 2017
Some of the most common issues that people run into are:
I'd eventually like to allow the order to differ from the table definition, but I'm not confident that we will ever be able to do that. While I do want to make sure that the errors from the compiler alone give at least enough information for experienced users to figure out what's going wrong, we can do better. I've often thought about trying to do more validation inside of
#[derive(Queryable)]. However, the issue there is that we can't get information about the code other than the item being annotated, meaning we can't ask things about the table mod. We also have no database connection, so we can't go to the schema to ask questions either.One issue I have with just validating from that derive though is that it assumes:
infer_schema!to define that tableAnd I've tried to avoid all of those assumptions in the design. So I think a lint makes a lot more sense, as it has access to way more information (meaning we don't need a database connection), and they can be much more granularly allowed by the user.
I'm unsure if these lints would be able to occur early enough to catch the most common cases, but we should definitely explore this option. Ideally I'd like to avoid using a database connection for this, and have it only affect types which implement
HasTable, find the module based on the value of that associated type, and base the linting off the value ofAllColumnsandSqlType. I'd like to try and provide a specific error message with suggestions on how to correct for all of the following, probably in this order:One other slightly less common mistake I've seen people make, which we could definitely catch with a lint is people thinking that they should be putting
diesel::types::Timestampon their struct directly.