In my own code I noticed that a common pattern like this:
let file = File::Create(file_name)?;
let mut writer = BufWriter::new(file);
can be more succinctly expressed as:
let mut writer = File::Create(file_name).map(BufWriter::new)?;
This seems like it would be an easy lint to implement and would be applicable any time a let-binded variable that came through a try! or operator? is immediately moved into a successive function. Usage of map or and_then could be suggested based on the whether the return type is a Result or not. If the chain is large there may be some small performance benefit to doing things this way due to the reduced number of implicit try! calls, but I'd have to measure this to be sure.
However there is an issue that many programmers may find the non-chained code to be more clear, so I'm not sure if it would be an appropriate lint for something like clippy.
However there is an issue that many programmers may find the non-chained code to be more clear, so I'm not sure if it would be an appropriate lint for something like clippy.
That's my worry as well. I don't think the community has really settled on a style. I like your first proposal as it's pretty much clear what's going on. I would even use shadowing for this, and call both bindings output or whatever this file will be used for.
Additionally, this is even more concise:
let mut output = BufWriter::new(File::create(file_name)?);
I'd argue that File::create(file_name).map(BufWriter::new)? is more idiomatic than BufWriter::new(File::create(file_name)?). Rust in general prefers method chains to nesting.
I prefer the first version, because it clearly shows which method might fail.
My opinion is that clippy shouldn't lint either way, at least not by default (tho opt-in is workable) - I doubt the community will ever settle on a style without a vocal minority. What you like depends on whether or not you are functionally minded or imperatively minded.
I just wanted to add my argument for why I prefer
let output = File::create(file_name)?;
let mut output = BufWriter::new(file);
that I forgot to mention in https://github.com/rust-lang-nursery/rust-clippy/issues/2271#issuecomment-351822436: I really enjoy being able to remove/add things and keep the diff short.
My opinion is that clippy shouldn't lint either way, at least not by default (tho opt-in is workable) - I doubt the community will ever settle on a style without a vocal minority.
I agree on defaults but perhaps this could be an opt-in lint, eg. something like "aggressive_chaining". The main goal would be to aid people in discovery of these cases, and in some cases discovery of the methodology. I'm not sure where something like that would fit and perhaps clippy isn't the right tool.
@olson-dan Then perhaps should be a lint in the other direction too so there's no bias ;) Let's call it agressive_try.
@Centril I think the name you're looking for is try_harder
Both of these could be a restriction lint, which are definitely a thing in clippy
Most helpful comment
@Centril I think the name you're looking for is
try_harder