How to disable "unnecessary path disambiguator" warning? - rust

I am generating code with a macro, which contains fully qualified type paths like this:
let vec: Vec::<String>;
Note the extra :: before <String>. This is necessary so that the same input token can be also be used for the constructor, by appending ::new():
Vec::<String>::new()
However, this produces warnings:
warning: unnecessary path disambiguator
--> src/main.rs:4:17
|
4 | let vec: Vec::<String>;
| ^^ try removing `::`
I can't remove the :: because then I get an error:
error: chained comparison operators require parentheses
--> src/main.rs:6:14
|
6 | vec = Vec<String>::new();
| ^^^^^^^^^^
|
= help: use `::<...>` instead of `<...>` if you meant to specify type arguments
= help: or use `(...)` if you meant to specify fn arguments
error[E0423]: expected value, found struct `Vec`
--> src/main.rs:6:11
|
6 | vec = Vec<String>::new();
| ^^^
| |
| did you mean `vec`?
| did you mean `Vec { /* fields */ }`?
How can I disable the warning just for this one line?

It is an open issue currently.
This lint is currently slipping these attributes like #![allow(warnings)]
Reference

Related

Rust nested lazy iteration and the interaction with mutable closures

Nested iteration combined with side-effects don't seem to work very well with Rust iterators. Here is an example:
fn main(){
let v1=vec![1];
let v2=vec![3];
let mut v3=vec![];
v1.iter().map(|x|{
v2.iter().map(|y|{
v3.push(*y);
})
});
}
This results in the following error:
error: captured variable cannot escape `FnMut` closure body
--> src/main.rs:6:5
|
4 | let mut v3=vec![];
| ------ variable defined here
5 | v1.iter().map(|x|{
| - inferred to be a `FnMut` closure
6 | / v2.iter().map(|y|{
7 | | v3.push(*y);
| | -- variable captured here
8 | | })
| |______^ returns a reference to a captured variable which escapes the closure body
I assume the problem has something to do with the laziness of iterators and a conflict between the two closures. What is causing the error? And how do I fix it?

Lazy join multiple DataFrames on a Categorical

trying to implement the SAMPLE of Lazy join multiple DataFrames on a Categorical:
use polars::prelude::*;
fn lazy_example(mut df_a: LazyFrame, mut df_b: LazyFrame) -> Result<DataFrame> {
let q1 = df_a.with_columns(vec![
col("a").cast(DataType::Categorical),
]);
let q2 = df_b.with_columns(vec![
col("b").cast(DataType::Categorical)
]);
q1.inner_join(q2, col("a"), col("b"), None).collect()
}
getting an error:
error[E0308]: mismatched types
--> src\main.rs:6:23
|
6 | col("a").cast(DataType::Categorical),
| ---- ^^^^^^^^^^^^^^^^^^^^^ expected enum `polars::prelude::DataType`, found fn item
| |
| arguments to this function are incorrect
|
= note: expected enum `polars::prelude::DataType`
found fn item `fn(Option<Arc<RevMapping>>) -> polars::prelude::DataType {polars::prelude::DataType::Categorical}`
note: associated function defined here
--> C:\Users\rnio\.cargo\registry\src\github.com-1ecc6299db9ec823\polars-lazy-0.23.1\src\dsl\mod.rs:555:12
|
555 | pub fn cast(self, data_type: DataType) -> Self {
| ^^^^
help: use parentheses to instantiate this tuple variant
|
6 | col("a").cast(DataType::Categorical(_)),
| +++
applied the suggested fix:
col("a").cast(DataType::Categorical()),
col("b").cast(DataType::Categorical()),
get next error:
error[E0061]: this enum variant takes 1 argument but 0 arguments were supplied
--> src\main.rs:7:23
|
7 | col("a").cast(DataType::Categorical()),
| ^^^^^^^^^^^^^^^^^^^^^-- an argument of type `Option<Arc<RevMapping>>` is missing
|
note: tuple variant defined here
--> C:\Users\rnio\.cargo\registry\src\github.com-1ecc6299db9ec823\polars-core-0.23.1\src\datatypes\mod.rs:707:5
|
707 | Categorical(Option<Arc<RevMapping>>),
| ^^^^^^^^^^^
help: provide the argument
|
7 | col("a").cast(DataType::Categorical(/* Option<Arc<RevMapping>> */)),
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
So its missing an argument for Categorial() ... even though it will not be used:
// The RevMapping has the internal state.
This is ignored with casts, comparisons, hashing etc.
https://docs.rs/polars/latest/polars/datatypes/enum.RevMapping.html
Any idea how to fix this?
Thanks
Thanks to #Dogbert :)
here is the working code:
fn lazy_example(mut df_a: LazyFrame, mut df_b: LazyFrame) -> Result<DataFrame> {
let q1 = df_a.with_columns(vec![
col("a").cast(DataType::Categorical(None)),
]);
let q2 = df_b.with_columns(vec![
col("b").cast(DataType::Categorical(None))
]);
q1.inner_join(q2, col("a"), col("b")).collect()
}

Bad enforcement of clippy rule?

I have disallowed unrelated shadowing in variable declarations.
But now this rule gives me an error on these two lines
let overflow: bool;
(self.standing, overflow) = self.standing.overflowing_add(reason.to_severity());
The linting error I get is:
error: `overflow` shadows a previous, unrelated binding
--> src/models/peer.rs:73:25
|
73 | (self.standing, overflow) = self.standing.overflowing_add(reason.to_severity());
| ^^^^^^^^
|
note: the lint level is defined here
--> src/lib.rs:1:9
|
1 | #![deny(clippy::shadow_unrelated)]
| ^^^^^^^^^^^^^^^^^^^^^^^^
note: previous binding is here
--> src/models/peer.rs:73:10
|
73 | (self.standing, overflow) = self.standing.overflowing_add(reason.to_severity());
| ^^^^^^^^^^^^^
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#shadow_unrelated
I'm using version 1.62.0 of the rust compiler.
My thoughts are that this behavior is a bug. The above lines should be allowed under this clippy rule. Am I wrong?
The problem can be seen here. Thanks to #Jmp for writing an this illustration of the problem.
This is a reported bug - rust-clippy#6141.
I think the problem is that the destructuring assignment is desugared into a let declaration that reuses the same span, and this causes clippy to think it has the same name and thus shadowing.

Why does adding graphql-client as dependency cause rover to break?

I am setting up this project to help me pass authorization data between each subgraphs. I have an authorization subgraph to gather the data so the router will have to make a graphql query to the authorization subgraph and then pass it along. I picked graphql_client dependency because it's the same one that Apollo uses and it seems to have a lot of support compare to the other Rust graphql clients however when I add this dependency, it breaks another dependency. These are the steps
set up context example project -> https://github.com/apollographql/router/tree/v0.1.0-preview.6/examples/context
add these as dependency in Cargo.toml
graphql_client = { version = "0.10.0", features = ["reqwest-blocking"] }
reqwest = { version = "0.11.10", features = ["blocking"] }
run cargo build
see error
Compiling launchpad v0.1.0 (https://github.com/apollographql/rover.git?rev=94141242ba34cf00cde9630fc4a6dcd05d4fa5da#94141242)
error[E0433]: failed to resolve: use of undeclared type `HeaderMap`
--> /Users/macuser/.cargo/git/checkouts/rover-efd9f422be37a06b/9414124/crates/launchpad/src/introspect/runner.rs:30:26
|
30 | let mut header_map = HeaderMap::new();
| ^^^^^^^^^ not found in this scope
|
help: consider importing this struct
|
1 | use reqwest::header::HeaderMap;
|
error[E0433]: failed to resolve: use of undeclared type `HeaderName`
--> /Users/macuser/.cargo/git/checkouts/rover-efd9f422be37a06b/9414124/crates/launchpad/src/introspect/runner.rs:33:13
|
33 | HeaderName::from_bytes(header_key.as_bytes())?,
| ^^^^^^^^^^ not found in this scope
|
help: consider importing this struct
|
1 | use reqwest::header::HeaderName;
|
error[E0433]: failed to resolve: use of undeclared type `HeaderValue`
--> /Users/macuser/.cargo/git/checkouts/rover-efd9f422be37a06b/9414124/crates/launchpad/src/introspect/runner.rs:34:13
|
34 | HeaderValue::from_str(&header_value)?,
| ^^^^^^^^^^^ not found in this scope
|
help: consider importing this struct
|
1 | use reqwest::header::HeaderValue;
|
error[E0659]: `reqwest` is ambiguous
--> /Users/macuser/.cargo/git/checkouts/rover-efd9f422be37a06b/9414124/crates/launchpad/src/introspect/runner.rs:6:5
|
6 | use reqwest::header::{HeaderMap, HeaderName, HeaderValue};
| ^^^^^^^ ambiguous name
|
= note: ambiguous because of multiple potential import sources
= note: `reqwest` could refer to a crate passed with `--extern`
= help: use `::reqwest` to refer to this crate unambiguously
note: `reqwest` could also refer to the module imported here
--> /Users/macuser/.cargo/git/checkouts/rover-efd9f422be37a06b/9414124/crates/launchpad/src/introspect/runner.rs:5:5
|
5 | use graphql_client::*;
| ^^^^^^^^^^^^^^^^^
= help: use `self::reqwest` to refer to this module unambiguously
Dependency tree is apollo router -> uplink -> rover (branch = geal/launchpad)
I posted this same issue on apollo/router repo and they said it'll be fix soon
https://github.com/apollographql/router/issues/937#issuecomment-1114995491
You need to configure your subgraph to receive it.

Why can't I pass a String from env::Args to Path::new?

Consider the following example:
use std::env;
use std::path::Path;
fn main() {
let args: Vec<_> = env::args().collect();
let out_path: String = args[2];
let _path = Path::new(out_path);
}
Here's the error I'm getting while compiling:
error[E0308]: mismatched types
--> main.rs:8:27
|
8 | let _path = Path::new(out_path);
| ^^^^^^^^
| |
| expected reference, found struct `std::string::String`
| help: consider borrowing here: `&out_path`
|
= note: expected type `&_`
found type `std::string::String`
Now if I follow compiler's suggestion, I get this:
error[E0507]: cannot move out of indexed content
--> main.rs:7:28
|
7 | let out_path: String = args[2];
| ^^^^^^^
| |
| cannot move out of indexed content
| help: consider using a reference instead: `&args[2]`
error: aborting due to previous error
Which, after applying the suggestion, leads me to the previous error:
error[E0308]: mismatched types
--> main.rs:7:28
|
7 | let out_path: String = &args[2];
| ^^^^^^^^
| |
| expected struct `std::string::String`, found reference
| help: consider removing the borrow: `args[2]`
|
= note: expected type `std::string::String`
found type `&std::string::String`
How can I understand the situation and solve the problem?
This was indeed an unfortunate sequence of suggestions (use a reference > remove that reference), but this was caused by the manual type ascription related to out_path.
You want a string slice, not an owned String:
let out_path: &str = &args[2];
This fits both the restriction of args (you can't move out of indexed content) and the requirements of Path::new, which requires a reference.
As for your comment, a clone() "fixes" the cannot move out of indexed content error because it doesn't require a move from the args vector - it copies an element from it instead. This fix is of course inferior to just borrowing it, which also works with Path::new.

Resources