From f22bce8098a3e451e1843adb6ef8154f6bb5fd56 Mon Sep 17 00:00:00 2001 From: Shashwat Gauniyal Date: Thu, 5 Mar 2026 14:30:54 +0000 Subject: [PATCH 1/4] chore: fix doc-test parsing errors in doc examples --- cli/src/lib.rs | 19 ++++++++++++++----- client/src/lib.rs | 3 +++ idl/src/build.rs | 8 +++++++- lang/attribute/access-control/src/lib.rs | 7 +++++-- lang/attribute/account/src/lib.rs | 4 ++-- lang/attribute/error/src/lib.rs | 2 ++ lang/attribute/event/src/lib.rs | 8 ++++++++ lang/attribute/program/src/lib.rs | 4 ++++ lang/derive/accounts/src/lib.rs | 2 ++ lang/derive/space/src/lib.rs | 2 ++ lang/src/accounts/account.rs | 10 ++++++++++ lang/src/accounts/account_loader.rs | 2 ++ lang/src/accounts/boxed.rs | 2 ++ lang/src/accounts/interface.rs | 2 ++ lang/src/accounts/interface_account.rs | 6 ++++++ lang/src/accounts/migration.rs | 2 ++ lang/src/accounts/option.rs | 2 ++ lang/src/accounts/program.rs | 2 ++ lang/src/context.rs | 1 + lang/src/lazy.rs | 2 ++ lang/src/lib.rs | 2 ++ 21 files changed, 82 insertions(+), 10 deletions(-) diff --git a/cli/src/lib.rs b/cli/src/lib.rs index 38f40c3d36..3f794ab53a 100644 --- a/cli/src/lib.rs +++ b/cli/src/lib.rs @@ -1555,9 +1555,12 @@ pub type Files = Vec<(PathBuf, String)>; /// Create files from the given (path, content) tuple array. /// /// # Example -/// -/// ```ignore -/// crate_files(vec![("programs/my_program/src/lib.rs".into(), "// Content".into())])?; +/// # fn main() -> anyhow::Result<()> { +/// # use std::path::PathBuf; +/// # let files = vec![(PathBuf::from("programs/my_program/src/lib.rs"), "// Content".to_string())]; +/// # crate::create_files(&files)?; +/// # Ok(()) +/// # } /// ``` pub fn create_files(files: &Files) -> Result<()> { for (path, content) in files { @@ -1586,8 +1589,14 @@ pub fn create_files(files: &Files) -> Result<()> { /// /// # Example /// -/// ```ignore -/// override_or_create_files(vec![("programs/my_program/src/lib.rs".into(), "// Content".into())])?; +/// ```rust +/// # fn main() -> anyhow::Result<()> { +/// # let temp_dir = tempfile::tempdir()?; +/// # let file_path = temp_dir.path().join("lib.rs"); +/// # let files = vec![(file_path, "// Content".to_string())]; +/// // override_or_create_files(&files)?; +/// # Ok(()) +/// # } /// ``` pub fn override_or_create_files(files: &Files) -> Result<()> { for (path, content) in files { diff --git a/client/src/lib.rs b/client/src/lib.rs index 81b3e6f577..1baae25f94 100644 --- a/client/src/lib.rs +++ b/client/src/lib.rs @@ -569,6 +569,7 @@ impl + Clone, S: AsSigner> RequestBuilder<'_, C, /// # Example /// /// ```ignore + /// # fn main() -> Result<(), Box> { /// program /// .request() /// // Regular accounts @@ -585,6 +586,8 @@ impl + Clone, S: AsSigner> RequestBuilder<'_, C, /// }]) /// .args(instruction::Initialize { field: 42 }) /// .send()?; + /// # Ok(()) + /// # } /// ``` #[must_use] pub fn accounts(mut self, accounts: impl ToAccountMetas) -> Self { diff --git a/idl/src/build.rs b/idl/src/build.rs index 97175ff711..cc1d772ef0 100644 --- a/idl/src/build.rs +++ b/idl/src/build.rs @@ -47,8 +47,14 @@ pub trait IdlBuild { /// /// # Example /// -/// ```ignore +/// ```rust, no_run +/// # use std::path::PathBuf; +/// # use anchor_lang_idl::build::IdlBuilder; +/// # fn main() -> Result<(), Box> { +/// # let path = PathBuf::from("programs/my_program"); /// let idl = IdlBuilder::new().program_path(path).skip_lint(true).build()?; +/// # Ok(()) +/// # } /// ``` #[derive(Default)] pub struct IdlBuilder { diff --git a/lang/attribute/access-control/src/lib.rs b/lang/attribute/access-control/src/lib.rs index 6d31da0cc9..6d1286dab7 100644 --- a/lang/attribute/access-control/src/lib.rs +++ b/lang/attribute/access-control/src/lib.rs @@ -20,16 +20,17 @@ use syn::parse_macro_input; /// pub fn create(ctx: Context, bump_seed: u8) -> Result<()> { /// let my_account = &mut ctx.accounts.my_account; /// my_account.bump_seed = bump_seed; +// / Ok(()) // This was missing. /// } /// } /// /// #[derive(Accounts)] -/// pub struct Create { +/// pub struct Create <'info> { // <'info> added /// #[account(init)] /// my_account: Account<'info, MyAccount>, /// } /// -/// impl Create { +/// impl Create <'_> { /// pub fn accounts(ctx: &Context, bump_seed: u8) -> Result<()> { /// let seeds = &[ctx.accounts.my_account.to_account_info().key.as_ref(), &[bump_seed]]; /// Pubkey::create_program_address(seeds, ctx.program_id) @@ -37,6 +38,8 @@ use syn::parse_macro_input; /// Ok(()) /// } /// } +/// +/// # fn main() {} /// ``` /// /// This example demonstrates a useful pattern. Not only can you use diff --git a/lang/attribute/account/src/lib.rs b/lang/attribute/account/src/lib.rs index bac320bf2d..9478f6fb9b 100644 --- a/lang/attribute/account/src/lib.rs +++ b/lang/attribute/account/src/lib.rs @@ -65,7 +65,7 @@ mod lazy; /// To enable zero-copy-deserialization, one can pass in the `zero_copy` /// argument to the macro as follows: /// -/// ```ignore +/// ```rust,ignore /// #[account(zero_copy)] /// ``` /// @@ -406,7 +406,7 @@ pub fn derive_zero_copy_accessor(item: proc_macro::TokenStream) -> proc_macro::T /// /// `#[zero_copy]` is just a convenient alias for /// -/// ```ignore +/// ```rust,ignore /// #[derive(Copy, Clone)] /// #[derive(bytemuck::Zeroable)] /// #[derive(bytemuck::Pod)] diff --git a/lang/attribute/error/src/lib.rs b/lang/attribute/error/src/lib.rs index 3f403de2ef..0c0bd03820 100644 --- a/lang/attribute/error/src/lib.rs +++ b/lang/attribute/error/src/lib.rs @@ -36,6 +36,8 @@ use syn::{parse_macro_input, Expr}; /// #[msg("This is an error message clients will automatically display")] /// Hello, /// } +/// +/// # fn main() {} /// ``` /// /// Note that we generate a new `Error` type so that we can return either the diff --git a/lang/attribute/event/src/lib.rs b/lang/attribute/event/src/lib.rs index ba5085998f..b61d08656d 100644 --- a/lang/attribute/event/src/lib.rs +++ b/lang/attribute/event/src/lib.rs @@ -97,6 +97,8 @@ pub fn event( /// pub data: u64, /// pub label: [u8; 5], /// } +/// +/// # fn main() {} /// ``` #[proc_macro] pub fn emit(input: proc_macro::TokenStream) -> proc_macro::TokenStream { @@ -147,6 +149,8 @@ pub fn emit(input: proc_macro::TokenStream) -> proc_macro::TokenStream { /// pub struct MyEvent { /// pub data: u64, /// } +/// +/// # fn main() {} /// ``` /// /// **NOTE:** This macro requires `ctx` to be in scope. @@ -205,6 +209,8 @@ pub fn emit_cpi(input: proc_macro::TokenStream) -> proc_macro::TokenStream { /// pub struct MyInstruction<'info> { /// pub signer: Signer<'info>, /// } +/// +/// # fn main() {} /// ``` /// /// The code above will be expanded to: @@ -219,6 +225,8 @@ pub fn emit_cpi(input: proc_macro::TokenStream) -> proc_macro::TokenStream { /// /// CHECK: Self-CPI will fail if the program is not the current program /// pub program: AccountInfo<'info>, /// } +/// +/// # fn main() {} /// ``` /// /// See [`emit_cpi!`](emit_cpi!) for a full example. diff --git a/lang/attribute/program/src/lib.rs b/lang/attribute/program/src/lib.rs index 5ad43d4baf..9db2b201c6 100644 --- a/lang/attribute/program/src/lib.rs +++ b/lang/attribute/program/src/lib.rs @@ -65,6 +65,8 @@ pub fn program( /// /// ```ignore /// use anchor_client::anchor_lang; +/// +/// # fn main() {} /// ``` /// /// # Example @@ -118,6 +120,8 @@ pub fn declare_program(input: proc_macro::TokenStream) -> proc_macro::TokenStrea /// pub struct MyIx<'info> { /// pub signer: Signer<'info>, /// } +/// +/// # fn main() {} /// ``` #[proc_macro_attribute] pub fn instruction( diff --git a/lang/derive/accounts/src/lib.rs b/lang/derive/accounts/src/lib.rs index f55643e3e5..8ea1f02c45 100644 --- a/lang/derive/accounts/src/lib.rs +++ b/lang/derive/accounts/src/lib.rs @@ -32,6 +32,8 @@ use syn::parse_macro_input; /// pub struct Initialize<'info> { /// ... /// } +/// +/// # fn main() {} /// ``` /// /// # Constraints diff --git a/lang/derive/space/src/lib.rs b/lang/derive/space/src/lib.rs index f0832e1f0e..810c14aeed 100644 --- a/lang/derive/space/src/lib.rs +++ b/lang/derive/space/src/lib.rs @@ -34,6 +34,8 @@ use syn::{ /// #[account(init, payer = payer, space = 8 + ExampleAccount::INIT_SPACE)] /// pub data: Account<'info, ExampleAccount>, /// } +/// +/// # fn main() {} /// ``` #[proc_macro_derive(InitSpace, attributes(max_len))] pub fn derive_init_space(item: TokenStream) -> TokenStream { diff --git a/lang/src/accounts/account.rs b/lang/src/accounts/account.rs index 0a6e71f366..0af803af70 100644 --- a/lang/src/accounts/account.rs +++ b/lang/src/accounts/account.rs @@ -78,6 +78,8 @@ use std::ops::{Deref, DerefMut}; /// pub authorized: bool /// } /// ... +/// +/// # fn main() {} /// ``` /// /// # Using Account with non-anchor programs @@ -132,6 +134,8 @@ use std::ops::{Deref, DerefMut}; /// &self.0 /// } /// } +/// +/// # fn main() {} /// ``` /// /// ## Out of the box wrapper types @@ -193,6 +197,8 @@ use std::ops::{Deref, DerefMut}; /// pub program_data: Account<'info, ProgramData>, /// pub system_program: Program<'info, System>, /// } +/// +/// # fn main() {} /// ``` /// /// This example solves a problem you may face if your program has admin settings: How do you set the @@ -212,6 +218,8 @@ use std::ops::{Deref, DerefMut}; /// pub struct Example { /// pub my_acc: Account<'info, TokenAccount> /// } +/// +/// # fn main() {} /// ``` /// to access token accounts and /// ```ignore @@ -221,6 +229,8 @@ use std::ops::{Deref, DerefMut}; /// pub struct Example { /// pub my_acc: Account<'info, Mint> /// } +/// +/// # fn main() {} /// ``` /// to access mint accounts. #[derive(Clone)] diff --git a/lang/src/accounts/account_loader.rs b/lang/src/accounts/account_loader.rs index 7e89edeba1..12d507e7ba 100644 --- a/lang/src/accounts/account_loader.rs +++ b/lang/src/accounts/account_loader.rs @@ -91,6 +91,8 @@ use std::ops::DerefMut; /// pub bar: AccountLoader<'info, Bar>, /// pub authority: Signer<'info>, /// } +/// +/// # fn main() {} /// ``` #[derive(Clone)] pub struct AccountLoader<'info, T: ZeroCopy + Owner> { diff --git a/lang/src/accounts/boxed.rs b/lang/src/accounts/boxed.rs index 5fea704ad1..b2f0484189 100644 --- a/lang/src/accounts/boxed.rs +++ b/lang/src/accounts/boxed.rs @@ -11,6 +11,8 @@ //! pub struct Example { //! pub my_acc: Box> //! } +//! +//! # fn main() {} //! ``` use crate::solana_program::account_info::AccountInfo; diff --git a/lang/src/accounts/interface.rs b/lang/src/accounts/interface.rs index 1f2129e6fb..56a09cedb3 100644 --- a/lang/src/accounts/interface.rs +++ b/lang/src/accounts/interface.rs @@ -53,6 +53,8 @@ use std::ops::Deref; /// pub program_data: Account<'info, ProgramData>, /// pub authority: Signer<'info>, /// } +/// +/// # fn main() {} /// ``` /// The given program has a function with which the upgrade authority can set admin settings. /// diff --git a/lang/src/accounts/interface_account.rs b/lang/src/accounts/interface_account.rs index 4665c36de8..b4868c7c1c 100644 --- a/lang/src/accounts/interface_account.rs +++ b/lang/src/accounts/interface_account.rs @@ -78,6 +78,8 @@ use std::ops::{Deref, DerefMut}; /// pub authorized: bool /// } /// ... +/// +/// fn main() {} /// ``` /// /// # Using InterfaceAccount with non-anchor programs @@ -146,6 +148,8 @@ use std::ops::{Deref, DerefMut}; /// pub struct Example { /// pub my_acc: InterfaceAccount<'info, TokenAccount> /// } +/// +/// # fn main() {} /// ``` /// to access token accounts and /// ```ignore @@ -155,6 +159,8 @@ use std::ops::{Deref, DerefMut}; /// pub struct Example { /// pub my_acc: InterfaceAccount<'info, Mint> /// } +/// +/// # fn main() {} /// ``` /// to access mint accounts. #[derive(Clone)] diff --git a/lang/src/accounts/migration.rs b/lang/src/accounts/migration.rs index fdf43abe22..db12045158 100644 --- a/lang/src/accounts/migration.rs +++ b/lang/src/accounts/migration.rs @@ -146,6 +146,8 @@ pub enum MigrationInner { /// pub my_account: Migration<'info, AccountV1, AccountV2>, /// pub system_program: Program<'info, System>, /// } +/// +/// # fn main() {} /// ``` #[derive(Debug)] pub struct Migration<'info, From, To> diff --git a/lang/src/accounts/option.rs b/lang/src/accounts/option.rs index cf6428dd9d..dfe5ad1383 100644 --- a/lang/src/accounts/option.rs +++ b/lang/src/accounts/option.rs @@ -6,6 +6,8 @@ //! pub struct Example { //! pub my_acc: Option> //! } +//! +//! # fn main() {} //! ``` use std::collections::BTreeSet; diff --git a/lang/src/accounts/program.rs b/lang/src/accounts/program.rs index f03f3a5395..0d7a152b7f 100644 --- a/lang/src/accounts/program.rs +++ b/lang/src/accounts/program.rs @@ -62,6 +62,8 @@ use std::ops::Deref; /// pub program_data: Account<'info, ProgramData>, /// pub authority: Signer<'info>, /// } +/// +/// # fn main() {} /// ``` /// The given program has a function with which the upgrade authority can set admin settings. /// diff --git a/lang/src/context.rs b/lang/src/context.rs index 0e24b21efa..c020ead368 100644 --- a/lang/src/context.rs +++ b/lang/src/context.rs @@ -167,6 +167,7 @@ where /// pub callee_authority: UncheckedAccount<'info>, /// pub callee: Program<'info, Callee>, /// } +/// # fn main() {} /// ``` pub struct CpiContext<'a, 'b, 'c, 'info, T> where diff --git a/lang/src/lazy.rs b/lang/src/lazy.rs index afe2146e57..7b83e7b192 100644 --- a/lang/src/lazy.rs +++ b/lang/src/lazy.rs @@ -17,6 +17,8 @@ use crate::{AnchorDeserialize, Pubkey}; /// pub struct MyStruct { /// field: u8, /// } +/// +/// # fn main() {} /// ``` pub trait Lazy: AnchorDeserialize { /// Whether the type is a fixed-size type. diff --git a/lang/src/lib.rs b/lang/src/lib.rs index c5599f60ed..df5a4c00f4 100644 --- a/lang/src/lib.rs +++ b/lang/src/lib.rs @@ -832,6 +832,8 @@ macro_rules! require_gte { /// pub enum MyError { /// SomeError /// } +/// +/// # fn main() {} /// ``` #[macro_export] macro_rules! err { From a675ac7b87f1b826e8826ce9fad7775f29ec0303 Mon Sep 17 00:00:00 2001 From: Shashwat Gauniyal Date: Tue, 10 Mar 2026 16:20:28 +0000 Subject: [PATCH 2/4] fix: address PR review - fix doctests and remove redundant fn main from ignore blocks --- cli/src/lib.rs | 17 ++++++++++------- lang/attribute/access-control/src/lib.rs | 20 +++++++++++++------- lang/attribute/error/src/lib.rs | 9 ++++++--- lang/attribute/event/src/lib.rs | 12 ++++-------- lang/attribute/program/src/lib.rs | 4 ---- lang/derive/accounts/src/lib.rs | 2 -- lang/derive/space/src/lib.rs | 4 ++-- lang/src/accounts/account.rs | 10 ---------- lang/src/accounts/account_loader.rs | 2 -- lang/src/accounts/boxed.rs | 4 +--- lang/src/accounts/interface.rs | 2 -- lang/src/accounts/interface_account.rs | 6 ------ lang/src/accounts/migration.rs | 2 -- lang/src/accounts/option.rs | 4 +--- lang/src/accounts/program.rs | 2 -- lang/src/accounts/signer.rs | 2 +- lang/src/context.rs | 1 - lang/src/lazy.rs | 2 -- lang/src/lib.rs | 5 +++-- 19 files changed, 41 insertions(+), 69 deletions(-) diff --git a/cli/src/lib.rs b/cli/src/lib.rs index 3f794ab53a..46608af735 100644 --- a/cli/src/lib.rs +++ b/cli/src/lib.rs @@ -1555,10 +1555,13 @@ pub type Files = Vec<(PathBuf, String)>; /// Create files from the given (path, content) tuple array. /// /// # Example -/// # fn main() -> anyhow::Result<()> { +/// +/// ```rust,no_run +/// # use anchor_cli::create_files; /// # use std::path::PathBuf; +/// # fn main() -> anyhow::Result<()> { /// # let files = vec![(PathBuf::from("programs/my_program/src/lib.rs"), "// Content".to_string())]; -/// # crate::create_files(&files)?; +/// # create_files(&files)?; /// # Ok(()) /// # } /// ``` @@ -1589,12 +1592,12 @@ pub fn create_files(files: &Files) -> Result<()> { /// /// # Example /// -/// ```rust +/// ```rust,no_run +/// # use anchor_cli::override_or_create_files; +/// # use std::path::PathBuf; /// # fn main() -> anyhow::Result<()> { -/// # let temp_dir = tempfile::tempdir()?; -/// # let file_path = temp_dir.path().join("lib.rs"); -/// # let files = vec![(file_path, "// Content".to_string())]; -/// // override_or_create_files(&files)?; +/// # let files = vec![(PathBuf::from("test.rs"), "// Content".to_string())]; +/// # override_or_create_files(&files)?; /// # Ok(()) /// # } /// ``` diff --git a/lang/attribute/access-control/src/lib.rs b/lang/attribute/access-control/src/lib.rs index 6d1286dab7..2807e470ca 100644 --- a/lang/attribute/access-control/src/lib.rs +++ b/lang/attribute/access-control/src/lib.rs @@ -20,26 +20,32 @@ use syn::parse_macro_input; /// pub fn create(ctx: Context, bump_seed: u8) -> Result<()> { /// let my_account = &mut ctx.accounts.my_account; /// my_account.bump_seed = bump_seed; -// / Ok(()) // This was missing. +/// Ok(()) /// } /// } /// /// #[derive(Accounts)] -/// pub struct Create <'info> { // <'info> added -/// #[account(init)] +/// pub struct Create<'info> { +/// #[account(init, payer = payer, space = 8 + 1)] /// my_account: Account<'info, MyAccount>, +/// #[account(mut)] +/// payer: Signer<'info>, +/// system_program: Program<'info, System>, /// } /// -/// impl Create <'_> { +/// #[account] +/// pub struct MyAccount { +/// bump_seed: u8, +/// } +/// +/// impl Create<'_> { /// pub fn accounts(ctx: &Context, bump_seed: u8) -> Result<()> { /// let seeds = &[ctx.accounts.my_account.to_account_info().key.as_ref(), &[bump_seed]]; /// Pubkey::create_program_address(seeds, ctx.program_id) -/// .map_err(|_| ErrorCode::InvalidNonce)?; +/// .map_err(|_| error!(ErrorCode::InvalidNonce))?; /// Ok(()) /// } /// } -/// -/// # fn main() {} /// ``` /// /// This example demonstrates a useful pattern. Not only can you use diff --git a/lang/attribute/error/src/lib.rs b/lang/attribute/error/src/lib.rs index 0c0bd03820..ad4cd167bb 100644 --- a/lang/attribute/error/src/lib.rs +++ b/lang/attribute/error/src/lib.rs @@ -36,8 +36,6 @@ use syn::{parse_macro_input, Expr}; /// #[msg("This is an error message clients will automatically display")] /// Hello, /// } -/// -/// # fn main() {} /// ``` /// /// Note that we generate a new `Error` type so that we can return either the @@ -68,7 +66,9 @@ pub fn error_code( /// Generates an [`Error::AnchorError`](../../anchor_lang/error/enum.Error.html) that includes file and line information. /// /// # Example -/// ```rust,ignore +/// ```ignore +/// use anchor_lang::prelude::*; +/// /// #[program] /// mod errors { /// use super::*; @@ -77,6 +77,9 @@ pub fn error_code( /// } /// } /// +/// #[derive(Accounts)] +/// pub struct Example {} +/// /// #[error_code] /// pub enum MyError { /// #[msg("This is an error message clients will automatically display")] diff --git a/lang/attribute/event/src/lib.rs b/lang/attribute/event/src/lib.rs index b61d08656d..3ecda48d5e 100644 --- a/lang/attribute/event/src/lib.rs +++ b/lang/attribute/event/src/lib.rs @@ -97,8 +97,6 @@ pub fn event( /// pub data: u64, /// pub label: [u8; 5], /// } -/// -/// # fn main() {} /// ``` #[proc_macro] pub fn emit(input: proc_macro::TokenStream) -> proc_macro::TokenStream { @@ -149,8 +147,6 @@ pub fn emit(input: proc_macro::TokenStream) -> proc_macro::TokenStream { /// pub struct MyEvent { /// pub data: u64, /// } -/// -/// # fn main() {} /// ``` /// /// **NOTE:** This macro requires `ctx` to be in scope. @@ -204,18 +200,20 @@ pub fn emit_cpi(input: proc_macro::TokenStream) -> proc_macro::TokenStream { /// # Example /// /// ```ignore +/// use anchor_lang::prelude::*; +/// /// #[event_cpi] /// #[derive(Accounts)] /// pub struct MyInstruction<'info> { /// pub signer: Signer<'info>, /// } -/// -/// # fn main() {} /// ``` /// /// The code above will be expanded to: /// /// ```ignore +/// use anchor_lang::prelude::*; +/// /// #[derive(Accounts)] /// pub struct MyInstruction<'info> { /// pub signer: Signer<'info>, @@ -225,8 +223,6 @@ pub fn emit_cpi(input: proc_macro::TokenStream) -> proc_macro::TokenStream { /// /// CHECK: Self-CPI will fail if the program is not the current program /// pub program: AccountInfo<'info>, /// } -/// -/// # fn main() {} /// ``` /// /// See [`emit_cpi!`](emit_cpi!) for a full example. diff --git a/lang/attribute/program/src/lib.rs b/lang/attribute/program/src/lib.rs index 9db2b201c6..5ad43d4baf 100644 --- a/lang/attribute/program/src/lib.rs +++ b/lang/attribute/program/src/lib.rs @@ -65,8 +65,6 @@ pub fn program( /// /// ```ignore /// use anchor_client::anchor_lang; -/// -/// # fn main() {} /// ``` /// /// # Example @@ -120,8 +118,6 @@ pub fn declare_program(input: proc_macro::TokenStream) -> proc_macro::TokenStrea /// pub struct MyIx<'info> { /// pub signer: Signer<'info>, /// } -/// -/// # fn main() {} /// ``` #[proc_macro_attribute] pub fn instruction( diff --git a/lang/derive/accounts/src/lib.rs b/lang/derive/accounts/src/lib.rs index 8ea1f02c45..f55643e3e5 100644 --- a/lang/derive/accounts/src/lib.rs +++ b/lang/derive/accounts/src/lib.rs @@ -32,8 +32,6 @@ use syn::parse_macro_input; /// pub struct Initialize<'info> { /// ... /// } -/// -/// # fn main() {} /// ``` /// /// # Constraints diff --git a/lang/derive/space/src/lib.rs b/lang/derive/space/src/lib.rs index 810c14aeed..fc6c5323dc 100644 --- a/lang/derive/space/src/lib.rs +++ b/lang/derive/space/src/lib.rs @@ -16,6 +16,8 @@ use syn::{ /// /// # Example /// ```ignore +/// use anchor_lang::prelude::*; +/// /// #[account] /// #[derive(InitSpace)] /// pub struct ExampleAccount { @@ -34,8 +36,6 @@ use syn::{ /// #[account(init, payer = payer, space = 8 + ExampleAccount::INIT_SPACE)] /// pub data: Account<'info, ExampleAccount>, /// } -/// -/// # fn main() {} /// ``` #[proc_macro_derive(InitSpace, attributes(max_len))] pub fn derive_init_space(item: TokenStream) -> TokenStream { diff --git a/lang/src/accounts/account.rs b/lang/src/accounts/account.rs index 0af803af70..0a6e71f366 100644 --- a/lang/src/accounts/account.rs +++ b/lang/src/accounts/account.rs @@ -78,8 +78,6 @@ use std::ops::{Deref, DerefMut}; /// pub authorized: bool /// } /// ... -/// -/// # fn main() {} /// ``` /// /// # Using Account with non-anchor programs @@ -134,8 +132,6 @@ use std::ops::{Deref, DerefMut}; /// &self.0 /// } /// } -/// -/// # fn main() {} /// ``` /// /// ## Out of the box wrapper types @@ -197,8 +193,6 @@ use std::ops::{Deref, DerefMut}; /// pub program_data: Account<'info, ProgramData>, /// pub system_program: Program<'info, System>, /// } -/// -/// # fn main() {} /// ``` /// /// This example solves a problem you may face if your program has admin settings: How do you set the @@ -218,8 +212,6 @@ use std::ops::{Deref, DerefMut}; /// pub struct Example { /// pub my_acc: Account<'info, TokenAccount> /// } -/// -/// # fn main() {} /// ``` /// to access token accounts and /// ```ignore @@ -229,8 +221,6 @@ use std::ops::{Deref, DerefMut}; /// pub struct Example { /// pub my_acc: Account<'info, Mint> /// } -/// -/// # fn main() {} /// ``` /// to access mint accounts. #[derive(Clone)] diff --git a/lang/src/accounts/account_loader.rs b/lang/src/accounts/account_loader.rs index 12d507e7ba..7e89edeba1 100644 --- a/lang/src/accounts/account_loader.rs +++ b/lang/src/accounts/account_loader.rs @@ -91,8 +91,6 @@ use std::ops::DerefMut; /// pub bar: AccountLoader<'info, Bar>, /// pub authority: Signer<'info>, /// } -/// -/// # fn main() {} /// ``` #[derive(Clone)] pub struct AccountLoader<'info, T: ZeroCopy + Owner> { diff --git a/lang/src/accounts/boxed.rs b/lang/src/accounts/boxed.rs index b2f0484189..2c0fdaa86d 100644 --- a/lang/src/accounts/boxed.rs +++ b/lang/src/accounts/boxed.rs @@ -8,11 +8,9 @@ //! # Example //! ```ignore //! #[derive(Accounts)] -//! pub struct Example { +//! pub struct Example<'info> { //! pub my_acc: Box> //! } -//! -//! # fn main() {} //! ``` use crate::solana_program::account_info::AccountInfo; diff --git a/lang/src/accounts/interface.rs b/lang/src/accounts/interface.rs index 56a09cedb3..1f2129e6fb 100644 --- a/lang/src/accounts/interface.rs +++ b/lang/src/accounts/interface.rs @@ -53,8 +53,6 @@ use std::ops::Deref; /// pub program_data: Account<'info, ProgramData>, /// pub authority: Signer<'info>, /// } -/// -/// # fn main() {} /// ``` /// The given program has a function with which the upgrade authority can set admin settings. /// diff --git a/lang/src/accounts/interface_account.rs b/lang/src/accounts/interface_account.rs index b4868c7c1c..4665c36de8 100644 --- a/lang/src/accounts/interface_account.rs +++ b/lang/src/accounts/interface_account.rs @@ -78,8 +78,6 @@ use std::ops::{Deref, DerefMut}; /// pub authorized: bool /// } /// ... -/// -/// fn main() {} /// ``` /// /// # Using InterfaceAccount with non-anchor programs @@ -148,8 +146,6 @@ use std::ops::{Deref, DerefMut}; /// pub struct Example { /// pub my_acc: InterfaceAccount<'info, TokenAccount> /// } -/// -/// # fn main() {} /// ``` /// to access token accounts and /// ```ignore @@ -159,8 +155,6 @@ use std::ops::{Deref, DerefMut}; /// pub struct Example { /// pub my_acc: InterfaceAccount<'info, Mint> /// } -/// -/// # fn main() {} /// ``` /// to access mint accounts. #[derive(Clone)] diff --git a/lang/src/accounts/migration.rs b/lang/src/accounts/migration.rs index db12045158..fdf43abe22 100644 --- a/lang/src/accounts/migration.rs +++ b/lang/src/accounts/migration.rs @@ -146,8 +146,6 @@ pub enum MigrationInner { /// pub my_account: Migration<'info, AccountV1, AccountV2>, /// pub system_program: Program<'info, System>, /// } -/// -/// # fn main() {} /// ``` #[derive(Debug)] pub struct Migration<'info, From, To> diff --git a/lang/src/accounts/option.rs b/lang/src/accounts/option.rs index dfe5ad1383..f9f491fbc1 100644 --- a/lang/src/accounts/option.rs +++ b/lang/src/accounts/option.rs @@ -3,11 +3,9 @@ //! # Example //! ```ignore //! #[derive(Accounts)] -//! pub struct Example { +//! pub struct Example<'info> { //! pub my_acc: Option> //! } -//! -//! # fn main() {} //! ``` use std::collections::BTreeSet; diff --git a/lang/src/accounts/program.rs b/lang/src/accounts/program.rs index 0d7a152b7f..f03f3a5395 100644 --- a/lang/src/accounts/program.rs +++ b/lang/src/accounts/program.rs @@ -62,8 +62,6 @@ use std::ops::Deref; /// pub program_data: Account<'info, ProgramData>, /// pub authority: Signer<'info>, /// } -/// -/// # fn main() {} /// ``` /// The given program has a function with which the upgrade authority can set admin settings. /// diff --git a/lang/src/accounts/signer.rs b/lang/src/accounts/signer.rs index 39a216305a..0b96015e80 100644 --- a/lang/src/accounts/signer.rs +++ b/lang/src/accounts/signer.rs @@ -25,7 +25,7 @@ use std::ops::Deref; /// /// #[derive(Accounts)] /// pub struct Example<'info> { -/// #[account(init, payer = payer)] +/// #[account(init, payer = payer, space = 8 + 8)] /// pub my_acc: Account<'info, MyData>, /// #[account(mut)] /// pub payer: Signer<'info>, diff --git a/lang/src/context.rs b/lang/src/context.rs index c020ead368..0e24b21efa 100644 --- a/lang/src/context.rs +++ b/lang/src/context.rs @@ -167,7 +167,6 @@ where /// pub callee_authority: UncheckedAccount<'info>, /// pub callee: Program<'info, Callee>, /// } -/// # fn main() {} /// ``` pub struct CpiContext<'a, 'b, 'c, 'info, T> where diff --git a/lang/src/lazy.rs b/lang/src/lazy.rs index 7b83e7b192..afe2146e57 100644 --- a/lang/src/lazy.rs +++ b/lang/src/lazy.rs @@ -17,8 +17,6 @@ use crate::{AnchorDeserialize, Pubkey}; /// pub struct MyStruct { /// field: u8, /// } -/// -/// # fn main() {} /// ``` pub trait Lazy: AnchorDeserialize { /// Whether the type is a fixed-size type. diff --git a/lang/src/lib.rs b/lang/src/lib.rs index df5a4c00f4..a070ca553f 100644 --- a/lang/src/lib.rs +++ b/lang/src/lib.rs @@ -832,8 +832,9 @@ macro_rules! require_gte { /// pub enum MyError { /// SomeError /// } -/// -/// # fn main() {} +/// +/// #[derive(Accounts)] +/// pub struct Example {} /// ``` #[macro_export] macro_rules! err { From 751a01a8fa793c3eb5afb51756f7e8278fc7c92c Mon Sep 17 00:00:00 2001 From: Shashwat Gauniyal Date: Tue, 10 Mar 2026 17:31:47 +0000 Subject: [PATCH 3/4] fix: unhide meaningful doctest --- cli/src/lib.rs | 8 ++++---- idl/src/build.rs | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/cli/src/lib.rs b/cli/src/lib.rs index 46608af735..1da9145383 100644 --- a/cli/src/lib.rs +++ b/cli/src/lib.rs @@ -1560,8 +1560,8 @@ pub type Files = Vec<(PathBuf, String)>; /// # use anchor_cli::create_files; /// # use std::path::PathBuf; /// # fn main() -> anyhow::Result<()> { -/// # let files = vec![(PathBuf::from("programs/my_program/src/lib.rs"), "// Content".to_string())]; -/// # create_files(&files)?; +/// let files = vec![(PathBuf::from("programs/my_program/src/lib.rs"), "// Content".to_string())]; +/// create_files(&files)?; /// # Ok(()) /// # } /// ``` @@ -1596,8 +1596,8 @@ pub fn create_files(files: &Files) -> Result<()> { /// # use anchor_cli::override_or_create_files; /// # use std::path::PathBuf; /// # fn main() -> anyhow::Result<()> { -/// # let files = vec![(PathBuf::from("test.rs"), "// Content".to_string())]; -/// # override_or_create_files(&files)?; +/// let files = vec![(PathBuf::from("test.rs"), "// Content".to_string())]; +/// override_or_create_files(&files)?; /// # Ok(()) /// # } /// ``` diff --git a/idl/src/build.rs b/idl/src/build.rs index cc1d772ef0..bc4d3bdc02 100644 --- a/idl/src/build.rs +++ b/idl/src/build.rs @@ -47,11 +47,11 @@ pub trait IdlBuild { /// /// # Example /// -/// ```rust, no_run +/// ```rust,no_run /// # use std::path::PathBuf; /// # use anchor_lang_idl::build::IdlBuilder; /// # fn main() -> Result<(), Box> { -/// # let path = PathBuf::from("programs/my_program"); +/// let path = PathBuf::from("programs/my_program"); /// let idl = IdlBuilder::new().program_path(path).skip_lint(true).build()?; /// # Ok(()) /// # } From 32acf0f7a877f2c3593894d4fb4e65fa4003e716 Mon Sep 17 00:00:00 2001 From: Shashwat Gauniyal Date: Wed, 11 Mar 2026 15:41:34 +0000 Subject: [PATCH 4/4] fix: address PR review - fix doctests, remove redundant fn main from ignore blocks --- client/src/lib.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/client/src/lib.rs b/client/src/lib.rs index 1baae25f94..81b3e6f577 100644 --- a/client/src/lib.rs +++ b/client/src/lib.rs @@ -569,7 +569,6 @@ impl + Clone, S: AsSigner> RequestBuilder<'_, C, /// # Example /// /// ```ignore - /// # fn main() -> Result<(), Box> { /// program /// .request() /// // Regular accounts @@ -586,8 +585,6 @@ impl + Clone, S: AsSigner> RequestBuilder<'_, C, /// }]) /// .args(instruction::Initialize { field: 42 }) /// .send()?; - /// # Ok(()) - /// # } /// ``` #[must_use] pub fn accounts(mut self, accounts: impl ToAccountMetas) -> Self {