@@ -422,6 +422,7 @@ impl<'a> PrefixComponent<'a> {
422422 /// See [`Prefix`]'s documentation for more information on the different
423423 /// kinds of prefixes.
424424 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
425+ #[ must_use]
425426 #[ inline]
426427 pub fn kind ( & self ) -> Prefix < ' a > {
427428 self . parsed
@@ -583,6 +584,7 @@ impl AsRef<Path> for Component<'_> {
583584///
584585/// [`components`]: Path::components
585586#[ derive( Clone ) ]
587+ #[ must_use = "iterators are lazy and do nothing unless consumed" ]
586588#[ stable( feature = "rust1" , since = "1.0.0" ) ]
587589pub struct Components < ' a > {
588590 // The path left to parse components from
@@ -609,6 +611,7 @@ pub struct Components<'a> {
609611///
610612/// [`iter`]: Path::iter
611613#[ derive( Clone ) ]
614+ #[ must_use = "iterators are lazy and do nothing unless consumed" ]
612615#[ stable( feature = "rust1" , since = "1.0.0" ) ]
613616pub struct Iter < ' a > {
614617 inner : Components < ' a > ,
@@ -1051,6 +1054,7 @@ fn compare_components(mut left: Components<'_>, mut right: Components<'_>) -> cm
10511054///
10521055/// [`ancestors`]: Path::ancestors
10531056#[ derive( Copy , Clone , Debug ) ]
1057+ #[ must_use = "iterators are lazy and do nothing unless consumed" ]
10541058#[ stable( feature = "path_ancestors" , since = "1.28.0" ) ]
10551059pub struct Ancestors < ' a > {
10561060 next : Option < & ' a Path > ,
@@ -1459,6 +1463,7 @@ impl PathBuf {
14591463 ///
14601464 /// [`capacity`]: OsString::capacity
14611465 #[ stable( feature = "path_buf_capacity" , since = "1.44.0" ) ]
1466+ #[ must_use]
14621467 #[ inline]
14631468 pub fn capacity ( & self ) -> usize {
14641469 self . inner . capacity ( )
@@ -2103,6 +2108,7 @@ impl Path {
21032108 /// assert_eq!(grand_parent.parent(), None);
21042109 /// ```
21052110 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
2111+ #[ must_use]
21062112 pub fn parent ( & self ) -> Option < & Path > {
21072113 let mut comps = self . components ( ) ;
21082114 let comp = comps. next_back ( ) ;
@@ -2169,6 +2175,7 @@ impl Path {
21692175 /// assert_eq!(None, Path::new("/").file_name());
21702176 /// ```
21712177 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
2178+ #[ must_use]
21722179 pub fn file_name ( & self ) -> Option < & OsStr > {
21732180 self . components ( ) . next_back ( ) . and_then ( |p| match p {
21742181 Component :: Normal ( p) => Some ( p) ,
@@ -2241,6 +2248,7 @@ impl Path {
22412248 /// assert!(!Path::new("/etc/foo.rs").starts_with("/etc/foo"));
22422249 /// ```
22432250 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
2251+ #[ must_use]
22442252 pub fn starts_with < P : AsRef < Path > > ( & self , base : P ) -> bool {
22452253 self . _starts_with ( base. as_ref ( ) )
22462254 }
@@ -2268,6 +2276,7 @@ impl Path {
22682276 /// assert!(!path.ends_with("conf")); // use .extension() instead
22692277 /// ```
22702278 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
2279+ #[ must_use]
22712280 pub fn ends_with < P : AsRef < Path > > ( & self , child : P ) -> bool {
22722281 self . _ends_with ( child. as_ref ( ) )
22732282 }
@@ -2303,6 +2312,7 @@ impl Path {
23032312 /// [`Path::file_prefix`]: Path::file_prefix
23042313 ///
23052314 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
2315+ #[ must_use]
23062316 pub fn file_stem ( & self ) -> Option < & OsStr > {
23072317 self . file_name ( ) . map ( rsplit_file_at_dot) . and_then ( |( before, after) | before. or ( after) )
23082318 }
@@ -2336,6 +2346,7 @@ impl Path {
23362346 /// [`Path::file_stem`]: Path::file_stem
23372347 ///
23382348 #[ unstable( feature = "path_file_prefix" , issue = "86319" ) ]
2349+ #[ must_use]
23392350 pub fn file_prefix ( & self ) -> Option < & OsStr > {
23402351 self . file_name ( ) . map ( split_file_at_dot) . and_then ( |( before, _after) | Some ( before) )
23412352 }
@@ -2360,6 +2371,7 @@ impl Path {
23602371 /// assert_eq!("gz", Path::new("foo.tar.gz").extension().unwrap());
23612372 /// ```
23622373 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
2374+ #[ must_use]
23632375 pub fn extension ( & self ) -> Option < & OsStr > {
23642376 self . file_name ( ) . map ( rsplit_file_at_dot) . and_then ( |( before, after) | before. and ( after) )
23652377 }
@@ -2403,6 +2415,7 @@ impl Path {
24032415 /// assert_eq!(path.with_file_name("var"), PathBuf::from("/var"));
24042416 /// ```
24052417 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
2418+ #[ must_use]
24062419 pub fn with_file_name < S : AsRef < OsStr > > ( & self , file_name : S ) -> PathBuf {
24072420 self . _with_file_name ( file_name. as_ref ( ) )
24082421 }
@@ -2660,6 +2673,7 @@ impl Path {
26602673 /// This is a convenience function that coerces errors to false. If you want to
26612674 /// check errors, call [`fs::metadata`].
26622675 #[ stable( feature = "path_ext" , since = "1.5.0" ) ]
2676+ #[ must_use]
26632677 #[ inline]
26642678 pub fn exists ( & self ) -> bool {
26652679 fs:: metadata ( self ) . is_ok ( )
@@ -2781,6 +2795,7 @@ impl Path {
27812795 /// Converts a [`Box<Path>`](Box) into a [`PathBuf`] without copying or
27822796 /// allocating.
27832797 #[ stable( feature = "into_boxed_path" , since = "1.20.0" ) ]
2798+ #[ must_use = "`self` will be dropped if the result is not used" ]
27842799 pub fn into_path_buf ( self : Box < Path > ) -> PathBuf {
27852800 let rw = Box :: into_raw ( self ) as * mut OsStr ;
27862801 let inner = unsafe { Box :: from_raw ( rw) } ;
0 commit comments