Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Potential false positive for let_and_return #1322

Closed
Keats opened this issue Nov 3, 2016 · 4 comments
Closed

Potential false positive for let_and_return #1322

Keats opened this issue Nov 3, 2016 · 4 comments
Labels
C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages E-medium Call for participation: Medium difficulty level problem and requires some initial experience. L-style Lint: Belongs in the style lint group

Comments

@Keats
Copy link

Keats commented Nov 3, 2016

I have the following macro:

#[macro_export]
macro_rules! try_get_value {
    ($filter_name:expr, $var_name:expr, $ty:ty, $val:expr) => {{
        let v: $ty = match ::serde_json::value::from_value($val.clone()) {
            Ok(s) => s,
            Err(_) => {
                return Err(::errors::TeraError::FilterIncorrectArgType(
                    $filter_name.to_string(),
                    $var_name.to_string(),
                    $val,
                    stringify!($ty).to_string())
                );
            }
        };
        v
    }};
}

And running clippy, it complains that I can directly return the match but doing so fails since it doesn't know the type ($ty can f32, String, etc).
Afaik there is no other way to cast the s variable other than that syntax (as $ty doesn't work). Am I missing a way to do it or is it a real false positive?

@oli-obk
Copy link
Contributor

oli-obk commented Nov 3, 2016

it's a false positive, but you can actually improve it in this case, by annotating the from_value function call

#[macro_export]
macro_rules! try_get_value {
    ($filter_name:expr, $var_name:expr, $ty:ty, $val:expr) => {{
        match ::serde_json::value::from_value::<$ty>($val.clone()) {
            Ok(s) => s,
            Err(_) => {
                return Err(::errors::TeraError::FilterIncorrectArgType(
                    $filter_name.to_string(),
                    $var_name.to_string(),
                    $val,
                    stringify!($ty).to_string())
                );
            }
        }
    }};
}

@Keats
Copy link
Author

Keats commented Nov 4, 2016

Embarrassing that I forgot to try that >_>

Thanks!
Do I leave the issue open for the false positive or close it?

@oli-obk
Copy link
Contributor

oli-obk commented Nov 4, 2016

leave it open, we need to improve the message

@oli-obk oli-obk added E-medium Call for participation: Medium difficulty level problem and requires some initial experience. C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages L-style Lint: Belongs in the style lint group labels Nov 4, 2016
@ebroto
Copy link
Member

ebroto commented Jun 4, 2020

I think this issue can be closed because let_and_return currently does not lint in the presence of type annotations (since 81b35d1)

// don't lint in the presence of type inference
if local.ty.is_none();

and it also avoids linting inside macros (since #5008)
if !in_macro(local.span);

@flip1995 flip1995 closed this as completed Jun 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages E-medium Call for participation: Medium difficulty level problem and requires some initial experience. L-style Lint: Belongs in the style lint group
Projects
None yet
Development

No branches or pull requests

4 participants