 So much for coming. Can you hear me in the back all right? Let's get started with some quick questions. Who if you knows about the Clippy project? Who knew about the Clippy project yesterday? All right. All right. That's still a very good number. Thank you so much okay so first things first Hi, I'm Pascal. I do rust since 2014 and If you ever happen to be in Cologne, please try to be there on the on the first once day of a month Because we do great rust meetups. You can also follow me on Twitter and I write some stuff in my blog about rust as well So you might want to check that out soon but Let's start with something. It's absolutely Everyone can agree with that rust programmers like compiler errors Yeah, I'm pretty sure that every one of you has seen a compiler error once in his or her lifetime and You set yourself. Wow. This is this is really great. I want to have more of those This basically what it looks it looks like it might be a bit small, but it's a very nice error format and this is something that delivers a promise of rust safety because The compiler is there to to help you check your stuff So you can be confident that your code actually works also the errors are pretty and It's not just errors. It's warnings to like We have a warning that you have not used the result of a function Like if you have a result type and you did not check it was an error It was an error if it's of any use the compiler will tell you This is not something that breaks your code. It's just something that points in the direction of maybe a mistake you made so your code gets even better and Am I absolutely favorite lint of all is missing documentation? If you're publishing a library and you activate this lint, it tells you which public items Have no documentation. This is not just so you can write documentation But it's also very very helpful to figure out which items are actually public and which items You did not want to be public and wanted to change later on and maybe he published a breaking change so it's very very useful for that and About two-thirds of you said you knew about clippy just for the other ones Clippy is awesome. You should totally look it up. It's a collection of even more lint So if you like the built-in errors, you can get even more errors like yesterday's there were 208 additional lints I am pretty sure if you check in next week, there'll be 210 or 12 or something like that and it goes more into the case of catching even more Mace mistakes that you may have missed for example if you wrote an infant else and Both code blocks is the same it's probably mistake on your part and you just copy pasted it from this to the else and Forgot to check it later on on fix it clippy has lint for that as well It's it's very very in isn't an ambitious project and it's very very awesome okay the completely other aspect of Lints and warnings is that you don't just want to catch warnings, but you also want to learn about new things For example, I hope you can read it. Otherwise, I'll just read it out loud to you so you have an iterator and you call the filter method on it and You give it a closure where you compare the item that you are iterating over with zero and Then the result of that you call next on so basically you have an iterator to get the next item that contains the number zero hmm It's an interesting way to to write that what what did you actually mean when you wrote that? It's it could be anything. It could be just a check for it. Is there any item that is zero Why not find The item and then you actually have explicitly stated in your code that you wanted to find an item that is zero or That's a trivial example. Let's go for something more. It's an if statement that if The map M does not contain the key K You insert K with the value V into M Okay It's all good. It's like how you insert stuff into a hash map in basically every language But Ross can help you with that In a way that may be surprising but will lead to very good code Have you heard the good news about the entry API? If you use dot entry instead of contains key and then insert you will do the hashing only once You ask the map is there an entry with that maps to K and if it's not there you can insert it this is very concise and very much not if then do stuff so you might not have even noticed that there is such an API and there's a link to tell you about it and Afterwards you'll be a Bit more experience in the crazy API is that Ross Senate library has to offer so This lends help you when you're basically almost using an API that you didn't know about and If you happen to be bought some lazy afternoon, you can also just check the clip your documentation because it has Examples for basically every lint was okay This is what you wrote and this is what you maybe have meant and this is what you could also write So good read I can recommend it Okay, okay, so I don't know how many of you are there from a tears talk earlier this morning he talked about I do magic rust and Man, he told you all about how to do it yourself It's it's a it's a nice idea to actually fix your coach yourself, but How about who makes us a bit more interactive? okay, so The compiler already gives you suggestions If you wrote for I in 10 dot dot zero you iterating over something that's never happening because Ranges only work in the one direction and not Counting downwards, so you were looking for Something that's called a reverse loop Basically, you wanted to say Zero dot dot ten and then reverse it You didn't know about it. Okay. It's totally fine. I didn't know about it either But the lint didn't just tell you okay here you can do this no no the lint actually wrote the code you need to write To get good idiomatic code So why fix it yourself This is from your perspective. I hope it's totally from my perspective The quote on the slides if you can't read it says, okay, I'm sold these suggestions are great But I don't make me type don't make me wait and just fix my freaking code already enter rust fix so There is no magic and it's already happening You've seen the errors. You've seen the suggestions There are messages and the diagnostics can also be outputted as jason instead of human readable text so let's just take this jason and Pass the suggestions and do a bit of search and replace and you end up with better code at least I hope you do It's actually if you think about really trivial and So the rust fix There's a tool that's called rust fix and it's a simple CLI tool you basically invoke it and it calls the compiler and asks hey Would you say what's this code and for every single? Error message you would have gotten from the compiler rust fix Collects the errors and shows you one by one. Hey So this is wrong or we can do better here. Would you like me to replace it with the dada day? And you say yeah sure or yeah, I don't know or This is plain wrong it's it's our lens there and After you ran through all these errors you say safe and it basically Replaces the things you've thought the totally to replace in your code and hopefully your code still compiles And it works better than before big promises I know But it's still not like if you can do better like you need to manually call this tool like it's one step too many Okay, okay What's better than like writing code and? Calling the compiler and making it tell us stuff and maybe basically wrapping rust fix around it well, obviously Let's do it in the editor while we are typing. We want to have suggestions as well and luckily for us People have already been working on that as well. It's called the rust language server and it's exposes warnings errors suggestions to an editor that can consume the language server the language server protocol the most complete plug-in for an editor is the one for a visual studio code and Turns out it can already fix our suggestions. So basically, why did we write a select tool? Just use this plug-in and So it turns out What's Clippy in this like the rust language server doesn't know what Clippy Why don't we make it know about Clippy and I had a horrible horrible hack that basically Imported lint from Clippy into the rust compiler that gets called by LS and Wasn't it wasn't pretty it really wasn't pretty so I was very glad to see me medicine. It was just this morning about hmm Why not add Clippy as an optional feature to the rust language server? So thanks to Oliver who's Here with us and whose talk is the one after mine, and you should totally stay for it Clippy is now an optional feature in the rust language server and Also five internet points for Nick Cameron It is already merged Not released but but much so if you are going to compile our as yourself you'll get this awesome bunch of additional lint So this is the time. Well, I'll give you a demo. Well, I don't I don't have my laptop But that's no problem. I expected to not have my laptop because well fine was too easy So let's do a quick slideshow. This is what it looks like the open visual study code and basically Right these lines just enable basic Clippy warnings right a main function Define a vector with the element. That's the number one And write an if statement that says hmm. What's the length? Is this length of the vector greater than zero? Hmm, what would you actually want to do here? Hmm length comparison to zero. That's a nice. That's a nice overlay, but also see there was a light bulb Okay, yeah, let's fix it and replace this if length greater zero with is not empty Because that's what we actually wanted to express and also this is more performant because collections Collections that are not as trivial as vector can basically implement is empty in a more efficient ways and counting their elements Okay, so we get a nice light bulb. We get a nice context menu and we can apply it and bam. It's done the plos is not the plos is absolutely not for me like I Hope the people who actually made this work are here or watching the stream Sometime in the future and this applause was for you Okay, time for another demo you wrote a new statement with a Small Boolean expression Just imagine it was like twice as long and not just something I came up with this morning It can be simplified To what? Oh, yeah, absolutely Less than or equal sure. We knew about that. Let's fix it damn another thing done but It's not all good like it's not all finished. It's not This is not 1.1.0 or anything near it and I want what I want you to take away from this is You can contribute This is a very nice lint that tells you that if you wanted to write 3.14 You may have actually wanted to write the constant for Pi so Why not use like The constant is actually defined Okay, wait, where's our light bulb? There is there is no light bulb Because this suggestion does not exist It's it's only zero message Okay, okay, so this was a demo. Thank you so much. I'm glad it works as well Let's talk a bit about how this actually works like not to zero lines of code. I committed just how it actually is working and Lints are a compiler plugins basically and if you've been here for the talk before mine Nikita talked about ASTs and lints basically are matches on particular AST structures you try to find notes and items of interest and Then you tell the compiler to basically annotate this Piece of code with the suggestion or the help text or a warning or what have you and the compiler then generates Output that's either human readable as you've seen before or machine readable So rust fix can't consume it This is not that hard as you like this is actually rather trivial. I managed to do it. So if you wanted to Look at some code and say hey, hmm, maybe maybe I can Can actually fix this Myself manually or by writing a lint you should totally not fix it yourself because everyone can do that write a lint We have a very Interesting question still open though because what is actually auto fixable like we have these suggestions and Can we apply all of them? This is everything we get good Codes that we can just search and replace Yeah, a lot of it is actually like I was surprised that I Expected it to like the very very small percentage of all suggestions we have That we could actually auto apply but it turns out we can apply auto fix a lot Which would totally make a list or actually How about the flag? Approximately suggestions so we know which ones are not auto fixable and When I wrote these lights earlier last week There wasn't was a PR open for the rust compiler. There's glorious pull request number 47,540 that acts as a simple flag to each suggestion you write and Passes this down to basically the JSON output as well and it allows you to flag approximate suggestions Yeah, I was gonna talk about how this is a good idea and how we could maybe do it, but actually It it got much already so just This stuff is changing rapidly like if you if you look at this talk in a year I hope you already already be using it and everything I tell you will be totally unsurprising So yeah, it's great progress. That's happening here and you should all contribute to it And with that, thank you so much and into recap just so I can leave this slide here search for rust clipping search for the good first issues and Become a compiler hacker. Yeah, let's start here and then go there Clippy is basically a compiler plugin as all ends are so It would be very very hard to stabilize Compilers internal APIs and not have some break ever so we don't want to do that, but actually Clippy will be shipped with rust up as a component you can install just like rust format or the LS So you don't have to so you can I think it's in in the next release Or maybe in the one after that that you can just write rust up component at clipping and get it on stable Just go there Okay, good good Yeah, so the question was if I have thought about fixes this introduce new errors Yeah, so a Lot of suggestions are not yet They they introduce Maybe not errors, but but new suggestions as this is a good case like we already producing codes that compiles but triggers new errors and Yeah, it's it's you could Get into the way of thinking that like okay Let's just recursively run rust fix Until it generates no more errors or it doesn't compile it. This is this is a good solution, but not one that's that's working Quite as we intended it to so I Have I have no solution to give you except for calling it multiple times But I'll be open to to any suggestions Any more questions. Yeah, sure. I don't I don't want to I don't want to say yes, but maybe I Don't I don't think so. I don't think so, but yeah, it's like Do you want to do that? Okay, okay We still have time for more questions. I guess that's actually the secret behind all my github contributions in the last year No, no, no, I've I've run it on on personal code. I have seen people run it on their code, but I Have not yet started like doing pull request Automated by rust fix it might might be a good idea actually, but you should totally look into it Yeah, the question was if I have tried or If I intended rust fix to be a tool that can upgrade code basically so once new rust features become available that you can use rust fix to upgrade your code base to use them and I totally didn't think about this until There was a discussion about introducing epochs to the epochs. Oh, they are called actually into rust because this year The rust team wants to basically compile a new version of the rust language itself and Introduce breaking changes without breaking code and they want to do that by actually versioning the compiler and the standard library in epochs so If you wanted to have a sync and to wait as a keyword in in random code This was this was actually proposed It would not work today because you can't introduce new keywords because people might already be using this for their variable names and Epochs will solve this because in your cargo configuration file you say, okay, let's use rust version 2018 instead of the implicit 2015 one. That's one point. Oh and in this discussion People started talking about well Can we make all these changes in a way that they can be auto applicable with rust fix? I was like Yes, please please do it and if there are some lens in clippy that Suggests using newer features or features that were not available in 1.0 so there's already a bit of precedence for it and I would be really happy to have a Rust fix that You can tell to load a lint group. That's called upgrade to 2018 and Get your code fixed Thank you